Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery html() function on future element

I have a form. When the user selects something specific in a dropdown that loads an external document which includes some additional fields like so

$( "#morefields" ).load( "partials/formadditionals.jsp", function() {
    ...
});

This "formadditionals.jsp" contains a title <h1 id="form2title"></h1>. Now after that has loaded I want to write something into that <h1> via

$('#form2title').html("Test");

...which does not work, I am assuming because the element did not exists before!?

How can I use the html() function on "future" elements after they are loaded into the DOM?

like image 721
DavidDunham Avatar asked Apr 08 '26 07:04

DavidDunham


1 Answers

Call it in the callback function.

$( "#morefields" ).load( "partials/formadditionals.jsp", function() {
    $('#form2title').html("Test");
});

Shameless plug: http://curtistimson.co.uk/jquery/using-jquery-callback-functions/

like image 54
Curtis Avatar answered Apr 10 '26 21:04

Curtis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!