Brian
Brian

Reputation: 294

Load Div created by script after page loaded.

How can I load a div after a page has been loaded and created by a script.

$(document).ready(function(){
   $('body').append('<div class="div_new" alt="hi">Hello World</div>');
});
$('.button').click(function(){
   alt_value = $('.div_new').attr('alt');
   alert(alt_value);
});

I'm trying to load a div created by a script.

Upvotes: 0

Views: 750

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337733

Your click handler needs to go inside the DOM ready handler. Without doing this, you will be trying to attach the click() to an element which doesn't yet exist. Try this:

$(document).ready(function(){
    $('body').append('<div class="div_new" alt="hi">Hello World</div>');

    $('.button').click(function(){
        alt_value = $('.div_new').attr('alt');
        alert(alt_value);
    });
});

Upvotes: 2

Related Questions