How to add / remove textbox dynamically with jQuery
In jQuery, it’s quite easy to add or remove a textbox dynamically. The idea is quite simple, just combine the use of ‘counter‘ variable, jQuery createElement(), html() and remove() method. See below example :
jQuery dynamic textbox example
<html> <head> <title>jQuery add / remove textbox example</title> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <style type="text/css"> div{ padding:8px; } </style> </head> <body> <h1>jQuery add / remove textbox example</h1> <script type="text/javascript"> $(document).ready(function(){ var counter = 2; $("#addButton").click(function () { if(counter>10){ alert("Only 10 textboxes allow"); return false; } var newTextBoxDiv = $(document.createElement('div')) .attr("id", 'TextBoxDiv' + counter); newTextBoxDiv.after().html('<label>Textbox #'+ counter + ' : </label>' + '<input type="text" name="textbox' + counter + '" id="textbox' + counter + '" value="" >'); newTextBoxDiv.appendTo("#TextBoxesGroup"); counter++; }); $("#removeButton").click(function () { if(counter==1){ alert("No more textbox to remove"); return false; } counter--; $("#TextBoxDiv" + counter).remove(); }); $("#getButtonValue").click(function () { var msg = ''; for(i=1; i<counter; i++){ msg += "\n Textbox #" + i + " : " + $('#textbox' + i).val(); } alert(msg); }); }); </script> </head><body> <div id='TextBoxesGroup'> <div id="TextBoxDiv1"> <label>Textbox #1 : </label><input type='textbox' id='textbox1' > </div> </div> <input type='button' value='Add Button' id='addButton'> <input type='button' value='Remove Button' id='removeButton'> <input type='button' value='Get TextBox Value' id='getButtonValue'> </body> </html>

how to add another texbox and get value of both text box when click on gettextbox value pls help!!!!!
how to add another texbox and get value of both text box when click on get textbox value button pls help
it helped me a lot
Hi Sir!
How will i save the textbox value using CI? Thanks!
How To Add / Remove … image ?
Hi,
There are two ways you can do it.
1) Simple “Hide” and “Show” images.
$(‘#btShow’).click(function() {
$(‘#imgTest’).show();
});
$(‘#btHide’).click(function() {
$(‘#imgTest’).hide();
});
2) Add or remove the images.
$(‘#btShow’).click(function() {
var ImgList = new Array();
ImgList[1] = ‘http://www.encodedna.com/images/utility.png’;
$(‘#imgTest’).attr(‘src’, ImgList[1]);
});
$(‘#btHide’).click(function() {
//$(‘#imgTest’).hide();
var imgBlank = ‘http://www.encodedna.com/images/blank.png’;
$(‘#imgTest’).attr(‘src’, imgBlank);
});
How to Handle validation for this text box? Please give the code for validation
sir please give some example struts2 with grid using jquery and struts2 with jquery(like show and hide textfiled when drop down list is use.
$(document).ready(function() {
$(‘#types’).change(function(){
if($(‘#types’).val() == ‘Other’)
{
$(‘#other’).show();
//document.getElementById(‘other’).disabled = true;
}
else
{
$(‘#other’).hide();
}
});
});
Registration Page
but it have no efect of show and hide
Hey is it possible to create next textbox when user hits enter button(on the keyboard). Say after giving some input in first textbox if the user hit enter key can we create next textbox. Hope u understood what i meant. By the thank u for u tutorial, is helping a lot.
Hi, I tried to retrieve the textbox value in the servlet by using getParameter()
However, it doesn’t work for the added textboxes (the first one is OK).
Does anyone know why? and how can I solve this problem?
Thanks in advance.
how to store these content in database.
you store the value in the hidden filed and then separate through thing and store in the database using split
dear sir
how to insert dynamic textbox values into database using jsp or javascript
Hi…
Thanks for this post..
Hi mani,
This is achieved by using jQuery ajax methods like $.get() or $.post().
For more details about jQuery ajax methods, visit http://api.jquery.com/category/ajax/
Aldrin.
Assuming that you have a button that will handle the submission with attribute id=”submit”:
Aldrin.
In process.php:
…could not post the next part, sorry…
this is not working with IE :(
how we will handle these multiple textboxes with php
if( isset( $_POST['textbox1'] ) ){
$value1=$_POST['textbox1'];}
else if ( isset( $_POST['textbox2'] ) ){
$value2=$_POST['textbox2'];}
etc….
Can someone just post the full code that works in the latest jquery. Mine wont work. No errors, just the buttons wont work
it should work on chrome,mozila ,opera and safari the above code. the only browser that doesn’t work is IE
after(‘</div')
For version after 1.4 ,there is another way apart from removing after() object ,
that is adding “” include after()
Nice code, however I did notice your example has 2 and tags.
Also, for those who want to use this code on jquery v1.7+ find this:
newTextBoxDiv.after().html(‘Textbox #’+ counter + ‘ : ‘ +
and replace with:
newTextBoxDiv.html(‘Textbox #’+ counter + ‘ : ‘ +
Cheers!
B
“2 and tags” should say 2 ‘head’ and ‘body’ tags.
Thanxs for your comment i was fighting with the error since 2 days ur awesome dude @Boris
Thank you bosss…. Save many hours…
Solutions do not work with jquery 1.7.1.
Any suggestion to update the code ?
Thx
Finally, the code works fine under jquery 1.7.1 without .after() and jQuery.noconflict()
WHAT IS jQuery.noconflict() ?
This is Very Good JQuery for adding New Field.
Very Nice………..
the code did not work for me too. i need the codes ASAP. tnx
it should work on chrome,mozila ,opera and safari the above code. the only browser that doesn’t work is IE
Great Tutorial Mannn
Thanx……………
How would i add css and style to the textboxes that are added dynamicaly?
Thank you very much! This script is a good start for my project. Thanks!
it works in IE?
Its really good and helped me..
Thanks for this! You saved my life! :)
How do i get the array variable into my php.
Working great but can someone show me how to get the msg variable into my php script?
WOW THIS IS AWESOME! just what i’ve been looking for.
Whew save me a lot of time :)
anyways..
can you please do a tutorial on how to save the fields value in php/mysql.
Please :)
i’ll wait for your response.
all of a sudden it’s not working on localhost? I need this.
I think it’s not working in the latest version of jQuery.
Hello I am javid Khan from siber Help .Thank you .It’s work Amazing .Nice post .
thanks nice work :) may I know how do I send it to other php page via POST? I’m a new PHP learner and I don’t know much about javascript.
Thanks in advance :)
!!thanks a tonne bro!!
Nice coding it’s working 100%,,
I have a small problem, i’m looking for a update for my site..
BOX –> value
(BOX)Length | (BOX)Height | (BOX)Base –> Add |Delete
want to add or delete and these box value should be calculate… have u any idea..
thanks
Buewanesh
Its working. just remove the “.after()” then it will work for jquery-1.4.4.min.js too.
Thanks!
Thank you! you saved my day.
Great!!
Thank you.. :))
Thanks, it works!!!
The code works great with Jquery 1.3.x but is not working with 1.4.2 it reports an error as follows:
Error: newTextBoxDiv.after() is undefined
Im not so much experience using jquery, so i cannot help as i would like. But, let me say, this code is great
Greetings
I was able to get it to work with jQuery 1.4.2 by removing the “.after()” and using newTextBoxDiv.html(‘<label…) instead of newTextBoxDiv.after().html('<label…). So far everything appears to be working just fine in 1.4.2 with the ".after()" removed
THX
very useful
Thank you
:)
thanks for this fix, had me stumped for half an hour before i decided to hit google :)
Thanks for the correction. This solves the problem!
This code is not working with latest version of jQuery (current version is 1.4.2). Can someone correct code so it’s working with latest version of jQuery
Hi,
How do i bind this textbox to a piece of data from database and where.
Please suggest.
Thanks
It’s depends on what framework or language you are using.
I m glad It works easily and very accurate.Good Job..
the code did not work for me.
read the first reply ;)
i was looking for something like this.
i could creat dynamically forms but i couldn’t get the valeus. Nice code, thx :)
You may consider substituting the “$” by “jQuery”