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?
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.
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With