5

This was a dublicate question, George's link to a previous question had my answer

I'm having an issue where selectors aren't functioning with dynamically generated javascript content.

The initial works just fine. Once the for loop generates more div's, even though it's got the same class, the 'mouseover' css styling won't apply.

Code that Generates the divs:

    for (x; x < y; x++) {
        output = output + '<div class="over">'+
            'But not for these generated divs'+ 
            '</div>';
    }
   $("#content").html(output);

Code that styles the divs with class "over":

$(".over").hover(function () {
    $(this).addClass("styling");
});


$(".over").mouseout(function () {
    $(this).removeClass("styling");
});

http://jsfiddle.net/kjhansen/1e08ypms/28/

Kevin H
  • 55
  • 6

1 Answers1

0

Use jQuery on() Try this:

$(document).on('mouseover','.over',function () {
    $(this).addClass("styling");
});


$(document).on('mouseout','.over',function () {
    $(this).removeClass("styling");
});

FIDDLE EXAMPLE

Think Different
  • 2,815
  • 1
  • 12
  • 18