1

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

3
  • Have you tried something simple like var formElement = document.createElement("form"); var divElement = document.createElement("div"); divElement.innerHTML = data; formElement.appendChild(divElement); document.body.appendChild(formElement);? Commented Mar 26, 2012 at 11:27
  • Can you show the same code not working using JSFiddle ? Commented Mar 26, 2012 at 11:28
  • Please found the below code I used for creating form tag --------- var formtag = document.createElement("form"); formtag.id = "frmtag1"; Commented Mar 27, 2012 at 5:31

1 Answer 1

1
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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.