JQuery selector does not work with dynamically generated content in IE7 and IE8

I have these elements on my site that are dynamically added to the jQuery function document.ready.

The problem is that I cannot select this element using regular jQuery selectors. JavaScript works fine in IE9 and other browsers. I think the reason is that this does not work, because the content I'm trying to change is being added dynamically.

How to solve this problem?

the code:

$('.dynamic').each(function(index)
    {
        $('textarea, input[type=radio], input[type=checkbox], select, input[type=text]', this).each(function()
        {

            var array = $(this).val().split('|||');

            var elements = new Array();

            var target = String('.dynamic_'+$(this).attr('id'));        

            $(target).each(function() //this does nothing in ie7 and 8, seems the target selector is messed up :S
            {
                elements.push($(this)); 
            });

            for (val in array)
            {
                var count = Number(val);    
                $(elements[count]).val(array[val]);         
            }
        });
    });
+5
source share
4 answers

javascript IE7 IE8, jQuery . , , .

live() delegate(). DOM .

:

$(".element").live("click", function() {  
    //dazzling stuff here
});

:

$('.element').delegate('.context-element', 'click', function() {  
    //dazzling stuff here 
});

deletage() live(), , live. , delegate() , , DOM, .

+6

You need to use live () for elements added at runtime: http://api.jquery.com/live/

0
source

If you are trying to select an item after dynamically adding inside document.ready, then it should work fine. If you are trying to select an element when it does not exist, and trying to attach event handlers, you should go for live.

0
source

All Articles