Dynamically add HTML fields to a web form using Javascript/JQuery

by Lasantha Samarakoon on Wednesday, May 18, 2011

Imagine a situation where you need to create a web form which allows users to upload images to a web gallery… In this case you need to allow them to upload multiple images simultaneously. As long as you don’t know how many images the user going to upload, the best way to implement it is create a form with one file field and put a button to dynamically append more file fields to the form.

If you are familiar with Javascript/JQuery, it will be an easy task. You can create it as follows. (Please note that I am using the JQuery library, as it simplifies the code rather than using pure Javascript.)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
// this function will be automatically called when the page is ready to display
$(document).ready(function() {
 // bind the button's click event with the function
 $('#add_button').bind('click', function() {
  // get the html content of the place holder and append new file field into it
  var newContent = $('#place_holder').html() + '<input type="file" name="image[]" value="" /><br />';
  // set the new html content to the place holder
  $('#place_holder').html(newContent);
 });
});
</script>
</head>
<body>
<form method="post" action="">
 <input type="button" value="Add new file" id="add_button" /><hr />
    <span id="place_holder">
     <input type="file" name="image[]" value="" /><br />
    </span>
</form>
</body>
</html>

As you can see, it is a really easy task. But when you put this code into action, you’ll notice there is a small problem with it. The problem is, whenever you add new file field, the files you already selected will lose. To solve it you have to do a little tweak to this code. The tweaked code is as follows.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
var counter = 1;
// this function will be automatically called when the page is ready to display
$(document).ready(function() {
 // bind the button's click event with the function
 $('#add_button').bind('click', function() {
  // generate html for new file field and create new place holder too (with new id)
  var newContent = '<input type="file" name="image[]" value="" /><br /><span id="next_position_' + (counter + 1) + '"></span>';
  // set the new html content to the place holder
  $('#next_position_' + counter).html(newContent);
  // increment the counter by one
  counter++;
 });
});
</script>
</head>
<body>
<form method="post" action="">
 <input type="button" value="Add new file" id="add_button" /><hr />
    <span id="file_set">
     <input type="file" name="image[]" value="" /><br />
        <span id="next_position_1"></span>
    </span>
</form>
</body>
</html>

In this method, we first create a place holder for the field. When the user clicks the button, we add the new file field as well as a new place holder with new ID into the current place holder. As long as this code does not re-write HTML for existing fields, contents of the fields will not affect anymore.

21 comments

Hey Nice post. I like it.

by Codespur on October 13, 2011 at 5:45 PM. #

Awesome post, I have bookmarked it for future reference.

by host1free on October 31, 2011 at 4:33 PM. #

very nice post

by phuket hotel on November 1, 2011 at 3:32 PM. #

Awesome thanks for nice posting!

by Sewanti on November 10, 2011 at 5:29 PM. #

Excellent posting!

by Custom Lanyards on November 11, 2011 at 9:03 PM. #

very good posting i like it

by BMX Accessories on November 12, 2011 at 6:42 PM. #

Thank you for your posting!
I like it

by Weihnachtsgeschenk Ideen on November 14, 2011 at 5:16 PM. #

your post is very nice. I like it very much.

by Himalayan Crystal Salt on November 25, 2011 at 6:23 PM. #

Your blog article is very intersting and fanstic,at the same time the blog theme is unique and perfect,great job.To

your success.

----------------------------------------------------------
Flower Girl Dresses|Empire Wedding Dresses|New Style Wedding Dresses

by Flower Girl Dresses on February 17, 2012 at 5:23 PM. #

Perfect breasts

A guy walking down the street sees a woman with perfect breasts.

He says to her "Hey Miss, would you let me bite your breasts for 100
dollars?

"Are you nuts?", she replies. And keeps walking away.

He turns around, runs around the block and gets to the corner before she
does. "Would you let me bite your breasts for 1,000 dollars?" he asks
again.

"Listen sir, I'm not that kind of woman. Got it?"

So the guy runs again around the next block and faces her again: "Would
you let me bite your breasts just once for 10,000 dollars?"

She thinks about it for a while and "Hmmm 10,000 dollars, eh? OK, just
once, but not here. Let's go to that dark alley over there".


So they went to that alley and she takes off the blouse to reveal the
most perfect breasts in the world. As soon as he sees them he jumps on them
and starts caressing them, fondling them, kissing them, burying his face in
them...but not biting.

In the end the woman gets all annoyed and asks: "Are you gonna bite them
or what?"

"Nah", he replies. "Costs too much."



Classic Dresses
Classic Bridesmaid Dresses
Wedding Dresses with Sleeves

by maple story mesos on February 21, 2012 at 9:14 AM. #

Good article.I really think what you said is right,in modern society,we have so many troubles,if you feel nothing to relax yourself,I suggest you to play the wow and some other games,and you can come to my page to find more information about http://www.mmolive.com/ and http://www.mmohome.com/gold/Guild-Wars-2-US.html/

by Happy smile make me strong on August 28, 2012 at 11:05 AM. #

Cheap Diablo 3 When you play with and base faithful when you can provide in addition to the roar resistance is to run non-stop to maintain their Saiyan.

by Anonymous on December 18, 2012 at 12:40 PM. #

hey, i have been looking for this kind of informative articles for such a long time, and i am really lucky to find out your work here.It is useful when i do that by myself or could make me know more clear on dealing with these issues. buy runescape gold , runescape gold

by Louyika Sia on January 7, 2013 at 10:58 AM. #

They are all beautiful and fashionable i have to say, and my friends told me they are of good quality as well. I have not see post good enough like yours here.
More tags:
wow gold for cheap

world of warcraft money

gold for wow cheap
cheap gold for wow

cheapest gold for wow
world of warcraft sale

wow gold cheap buy





by Popular shoes on January 14, 2013 at 2:14 PM. #

Imagine a situation where you need to create a web form which allows users to upload images to a web gallery… In this case you need to allow them to upload multiple images simultaneously. As long as you don’t know how many images the user going to upload, the best way to implement it is create a form with one file field and put a button to dynamically append more file fields to the formswtor gold
buy swtor gold
cheap swtor gold
tor credits
buy tor credits
cheap tor credits

by Anonymous on January 17, 2013 at 8:57 AM. #

In this method, we first create a place holder for the field. When the user clicks the button, we add the new file field as well as a new place holder with new ID into the current place holder. As long as this code does not re-write HTML for existing fields, contents of the fields will not affect anymore.guild wars 2 gold
buy guild wars 2 gold
cheap guild wars 2 gold
cheapest guild wars 2 gold
guild wars 2 gold for sale

by Anonymous on January 17, 2013 at 9:02 AM. #

I don’t even know how I found themselves listed here, nonetheless believe this information is shocking.
I personally don’t recognise what you do having said that surely you will definitely be considered a famous digg, if you're not presently.
Also visit my web blog : Genital Warts Home Remedy

by Anonymous on January 23, 2013 at 8:53 AM. #

Thank you so much for letting us know about this! I must say that you are a very dedicated person to have written a wonderful post like this!

by aion accounts on January 31, 2013 at 3:26 PM. #

Hey! I could have sworn I've been to this blog before but after browsing through some of the post I
realized it's new to me. Anyways, I'm definitely happy I found it and I'll be bookmarking and checking back
frequently!

Check out my webpage ... View my web page
My webpage - Please visit my site

by Anonymous on March 12, 2013 at 6:48 AM. #

Hey there! I've been reading
your weblog for a while now and
finally got the courage to go ahead and give you a shout out from Austin
Texas! Just wanted to mention keep up the
good work!

Review my site Click here

by Anonymous on March 16, 2013 at 10:31 AM. #

That is really cool. Thanks for finding it. Some really good ideas there, this is going to be a real time sink. I am a china tour lover,You can learn more: China vacation packages | China tour operator | China travel packages

by china tours on April 28, 2013 at 11:39 AM. #

Leave your comment