BHANU PRAKASH INTURI
BHANU PRAKASH INTURI

Reputation: 57

How to create form tag dynamically using javascript

I am able to create div tag dynamically but unable to create form tag dynamically. the task is I have to create form tag in that i have to create div tag dynamically. Below is the code that create div tag dynamically

var divTag = document.createElement("div");
document.body.appendChild(divTag).innerHTML = data;

Please help me to create form tag in that div tag dynamically using javascript

Upvotes: 1

Views: 4712

Answers (1)

Sirko
Sirko

Reputation: 74086

var divTag = document.createElement("div");
var formTag = document.createElement( 'form' );

divTag.innerHTML = data;
divTag.appendChild( formTag );

document.body.appendChild(divTag);

I don't know, what kind of data you assign to innerHTML, but the above code would append the form tag after that data.


Alternativly you could build up a whole string with all the content for that div and set innerHTML once.

var divTag = document.createElement("div");

data += '<form action=""></form>';

divTag.innerHTML = data;
document.body.appendChild(divTag);

Upvotes: 1

Related Questions