Css in all php/mysql generated Ivs

Asked

Viewed 80 times

0

How to do when the mouse enters the div it increases the opacity to 1.0 and when it exits the div back to previous opacity

Example:

PHP/MYSQL

$query = mysql_query("SELECT * FROM bd) or die(mysql_error());

while ($row = mysql_fetch_array($query)) {
echo "
<table border=0 width=\"720\">
<tr id=\"div\">
<td width=\"382\">
TEXTO
</td> 
</tr></table><br><br>";
}

JS

<script>
$("#div").mouseover(function() {
$("#div").css("opacity","1.0");
});

$("#div").mouseout(function() {
$("#div").css("opacity","0.8");
});
</script>

in the second div coming from db js does not work, only in the first. tried

<script>
$("#div").mouseover(function() {
$("#div").next().css("opacity","1.0");
});
</script>

but it didn’t work......

  • 2

    Never use the same id for more than one element , id's are unique

  • Did you see my Dit on the answer about delegating events? tested this?

2 answers

4

To add CSS to dynamically generated elements have two options:

#1 - give this element a class and add the rules of that class to your CSS

So in PHP you add the newElement class

while ($row = mysql_fetch_array($query)) {
    echo "
    <table border=0 width=\"720\">
        <tr id=\"div\" class=\"newElement\">
            <td width=\"382\">
                TEXTO
            </td> 
        </tr>
    </table><br><br>";
}

and in the CSS: .newElement{ opacity: 1;}

#2 - run the code you used above just after the elements were added, ie within the ajax onSuccess function

success: function (data) {
    $("#esteID").css("opacity","1.0");
    // ou então no elemento parente comum: $("#parente div").css("opacity","1.0");
}

Note: Ids must be unique. In your while in PHP it looks like you’re using the same ID. This gives errors in the code, the most common rule is to be applied only to the first element that has the ID you want. If you want to use the same rules on multiple elements, then use class. If you want to generate Ids dynamically within your while can add a counter that adds an extra number to your ID. But to give you an exact example I need to know better how you use ajax.


EDIT:

After seeing your answer I realized that the wig was poorly made (now corrected) and that what you need is to use event delegation because Event Handler was run before html was on the page. So use it like this:

$(document).on('mouseenter', '.div', function () {
    $(this).css("opacity", 1);
});
$(document).on('mouseleave', '.div', function () {
    $(this).css("opacity", "0.8");
});

0

I got it this way:

PHP

$query = mysql_query("SELECT * FROM bd) or die(mysql_error());

while ($row = mysql_fetch_array($query)) {
echo "
<table border=0 width=\"720\">
<tr id=\"div\" class=\"div\">
<td width=\"382\">
TEXTO
</td> 
</tr></table><br><br>";
}

JS

$(".div").mouseover(function() {
$(this).css("opacity","1.0").next(".div").css("opacity","1.0");
});

$(".div").mouseout(function() {
$(this).css("opacity","0.8").next(".div").css("opacity","0.8");
});

Using css(), next() then css() again...

OR using CSS only:

#div {
opacity: 0.8;
}

#div:hover {
opacity: 1.0;
}
  • Okay, so you gave a class as I suggested, but does the actual action of adding the CSS have to be done manually with a mouseover? was that what you intended? And by the way, it still has duplicate Ids.

  • The duplicate id is no problem, I could have done with css as well, example: . div:Hover { opacity: 1.0; }

  • Your question was: Como adicionar css em todas as divs vindas do php/mysql ? I don’t see how your answer effectively solves the problem. Either the question is wrong/incomplete or your answer is. The problem of duplicate Ids must be solved.

  • I think the question was poorly asked, I simply wanted when the mouse enters the div it increases the opacity to 1.0 and when it exits the div back to previous opacity...

  • I actually knew the answer, it was that I had forgotten the basics but I remembered :)

  • Okay, I edited my answer with the solution to your problem. You have to use event delegation and the class idea.

  • Thanks @Sergio, I edited the question too...

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.