<HTML>
<HEAD>
<TITLE>Dynamically add Textbox, Radio, Button in html Form using JavaScript</TITLE>
<SCRIPT language="javascript">
function add(type) {
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute("type", type);
element.setAttribute("value", type);
element.setAttribute("name", type);
var foo = document.getElementById("fooBar");
//Append the element in page (in span).
foo.appendChild(element);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<H2>Dynamically add element in form.</H2>
Select the element and hit Add to add it in form.
<BR/>
<SELECT name="element">
<OPTION value="button">Button</OPTION>
<OPTION value="text">Textbox</OPTION>
<OPTION value="radio">Radio</OPTION>
</SELECT>
<INPUT type="button" value="Add" onclick="add(document.forms[0].element.value)"/>
<span id="fooBar"> </span>
</FORM>
</BODY>
</HTML>
Code language: HTML, XML (xml)
Also note that we have used setAttribute() method to assign the attributes to our dynamically created element. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
This works fine but it is important to note that the setting of the "name" attribute will fail in IE. It sets the name for the form submission, but does NOT set it in terms of javascript access (you won't be able to access it by name).
http://webbugtrack.blogspot.com/2008/03/bug-240-setattribute-name-is-half.html
Thanks Harold for the information. I din't knew that.
How can use the added text box value in php?
description:
the text box will be dynamically added by user and then the added text box value posted to php file.
Hi Saran,
Use normal html form to post the textbox value in PHP script. The dynamically created textbox's value can be access in PHP using normal $_GET["textboxname"] variable in PHP.
Hi Viral,
How to label those text boxes,buttons and radio buttons?
Hi Angeline,
To add labels to textboxes, buttons etc you can add text to the parent attribute.
For example:
hi...
I have text box A1, B1, C1. I click ADD and the table will append and text box A2, B2, B3 appeared.... and the next row appear when i click ADD again. but i have a button name SUM, when i click SUM, how can i make it to be A1+B1 and the result appear in C1 and also to A2, B2, B3 and below...
Hi jAi,
For implementing such a functionality, you may have to use javascript dom manipulation heavily.
First you may iterate through all the rows of table, get pointer to textboxes Ax and Bx, where x is the row id. Then sum it and update Cx. If I implement such code, my code would be:
How to select and deselect the radio elsement created by your code????????????//
@Chirag,
I think you are using Internet Explorer to view the example.
IE has a known bug (http://webbugtrack.blogspot.com/2008/03/bug-240-setattribute-name-is-half.html) where setAttribute (name) does not works.!! You can try running this code in Firefox.