Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a Form Dynamically Via Javascript

My sites Aweber registration forms are getting a lot of spam. I was told to create the forms dynamically via javascript after page has rendered or via clicking button. How would I create and render a form via javascript?

like image 579
Nick Avatar asked Aug 06 '11 05:08

Nick


People also ask

Can you create a form in JavaScript?

Approach 1: Use document. createElement() to create the new elements and use setAttribute() method to set the attributes of elements. Append these elements to the <form> element by appendChild() method. Finally append the <form> element to the <body> element of the document.

How do you create a dynamic input field in HTML?

If you want to dynamically add elements, you should have a container where to place them. For instance, a <div id="container"> . Create new elements by means of document. createElement() , and use appendChild() to append each of them to the container.

How do I create a dynamic form in react JS?

The values will be input.name and input.Create a function called handleFormChange. Assign this function to the input fields as an onChange event. This onChange event takes two parameters, index and event. Index is the index of the array and event is the data we type in the input field.

Can HTML elements be create dynamically using JavaScript?

New elements can be dynamically created in JavaScript with the help of createElement() method. The attributes of the created element can be set using the setAttribute() method.


1 Answers

some thing as follows ::

Add this After the body tag

This is a rough sketch, you will need to modify it according to your needs.

<script> var f = document.createElement("form"); f.setAttribute('method',"post"); f.setAttribute('action',"submit.php");  var i = document.createElement("input"); //input element, text i.setAttribute('type',"text"); i.setAttribute('name',"username");  var s = document.createElement("input"); //input element, Submit button s.setAttribute('type',"submit"); s.setAttribute('value',"Submit");  f.appendChild(i); f.appendChild(s);  //and some more input elements here //and dont forget to add a submit button  document.getElementsByTagName('body')[0].appendChild(f);  </script> 
like image 128
Pheonix Avatar answered Oct 07 '22 10:10

Pheonix