Colenso Castellino
Colenso Castellino

Reputation: 900

Best way to dynamically build complex HTML elements returned by Server

I have a <div> which should get populated with a form on Mouse click.

I have lined up the jQuery for this by sending the request to the server and the server returning me the form as simple text. However if I use innerHTML to add the contents to the will it be accessible to say, the Validation plugin of jQuery to validate the form that was sent by the server and inserted into the <div>.

I know of a method of manually creating each DOM element but the form is too complex to be created manually!

Which method would suit my situation the best?

Upvotes: 1

Views: 415

Answers (1)

charlietfl
charlietfl

Reputation: 171669

Using validation plugin as example, you simply call the plugin within the succcess callback of the ajax, once the new html exists.

Simplified version using jQuery load() ajax shortcut method

$('#contentDiv').load('formUrl.php',function(){
    /* form now exists, can add validation*/
    $('#myNewForm').validate( /* ootions object */);

})

Upvotes: 2

Related Questions