Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create form and form element dynamically?

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one tell me how to create form and form elements dynamically?
Once the form is ready, i have to submit it dynamically..

I have tried, but failed to get the expected result.

here is the code which i have tried..

function buildForm()
{
var form_ref=document.createElement("form");
form_ref.name="addform";
form_ref.action="a.jsp";
form_ref.method="post";
form_ref.target="new";

var hidden_element = document.createElement("input");
hidden_element.type="hidden";
hidden_element.name="id";
hidden_element.value="1";
form_ref.appendChild(hidden_element);

var submit_element = document.createElement("input");
submit_element.type="submit";
submit_element.name="submit";
submit_element.value="submit";
form_ref.appendChild(submit_element);


form_ref.submit();
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64848
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and many people that might be able to help you will just move along. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64848
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point do you attach the form to the document?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look at creating elements this way. The name has issues being set in certain browsers. The code here:
http://www.pascarello.com/examples/createElement.html

fixes that browser quirk.

As Bear said you need to look into appendChild so the form is actually added to the page so you can submit it.

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic