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.

23 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. #

Fantastic beat ! I wish to apprentice while you amend your web site,
how could i subscribe for a blog web site? The account helped me a acceptable
deal. I had been tiny bit acquainted of this your broadcast offered bright
clear concept

Also visit my web-site: viagra *viagrabritain.co.uk*

by Anonymous on June 11, 2013 at 6:29 AM. #

陽痿 男人心里的潛意識!

最近,德國著名心理學家提壯陽藥 壯陽藥出一個新的觀點,他認威而鋼 犀利士 威而鋼 犀利士 犀利士為男人有陽痿,是因為傳統觀念對男性的要求往往過于苛刻,尤其是在過性生活的時候,男性是不允許有失敗的。
  男性一旦出現陽痿就會被別人譏笑,更會使自己背上沉重的心理負擔。
  專家研究發現,真正因為身體發生病理性改變導致陽痿的患者,其實是少之又少的;出現陽痿大多是心理上出現了問題。
  心理學家還提醒男性,想做愛和應該做愛是兩壯陽藥 威而鋼 犀利士 犀利士專賣 犀利士 犀利士5mg價格 壯陽藥品 犀利士專賣 威而鋼 壯陽藥回事。我們通常所說的陽痿,是男性在過性生活的時候,性器官無法勃起,實際上那時無法勃起的男性可能僅僅是因為覺得自己應該過性生活,并沒有生理上的需求。
  很多人的潛意識里常常會有這樣的觀念,認為男性的性欲是隨時可以喚起的,否則就是出了問題。
  但是現代的性醫學認為,男性的性欲也有較為明顯的周期,雖然男性的性欲比其他意識更敏銳,但是如果性生活的興趣被破壞的話,性意識和性器官就會作出反犀利士 犀利士 犀利士 犀利士 犀利士 威而鋼 威而鋼 威而鋼 威而鋼哪裡買 威而鋼 威而鋼 威而鋼 威而鋼 犀利士 壯陽藥品去哪買 犀利士 犀利士 犀利士 犀利士 犀利士應,所以陽痿的病因,通常是來自男性的心靈深處。
  心理學家還指出,男性的性沖動與原始進攻欲和占有欲是密切相連的,與忽略性行為和初級欲望的滿足也有關系。對性器官太長或者太短的在意、對避孕常識的匱乏等等因素,都可能使男性出現暫時性的陽痿。
  一位善解人意的女性,應該成為治療男性陽痿威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 威而鋼 犀利士哪裡買 壯陽藥品 壯陽藥 威而鋼 威而鋼哪裡買 威而鋼專賣店 威而鋼藥局 情色貼圖的良師益友。心理學家指出,沒有什么比女性的溫柔更能化解男性心靈上的障礙了,所以男性朋友一旦對自己的性功能產生懷疑的時候,不要過于擔心,和愛人及時溝通交流,這才是最重要的。

by 橡教官 on February 10, 2014 at 2:32 PM. #

Leave your comment