Button to add additional fields to HTML Form

How could I use a button to "Add" additional RSS Title and RSS Link fields to the end of my form?  I assume with jQuery

<form class="col-md-12" action="submit.php" method="post" style="margin-top:20px;margin-bottom:20px;">

                        <div class="form-group">
                            <label><span class="accountHead">Custom Insights</span></label><br><br>
                            <input type="radio" name="insightsMode" value="0"> Disabled
                            <input type="radio" name="insigntsMode" value="1" checked=""> Enabled
                        </div>

                        <div class="form-group">
                            <label>RSS Feeds</label><div class="clearfix"></div>
                            <div class="col-md-6 noPadLeft">
                                <input class="form-control" name="rssInfo[title]" placeholder="Title"/>
                            </div>
                            <div class="col-md-6 noPadLeft">
                                <input class="form-control" name="rssInfo[link]" placeholder="Link"/>
                            </div>
                            <div class="clearfix"></div>
                        </div>

                        <input type="hidden" name="submitType" value="20"/>
                        <input type="hidden" name="userID" value="<?php echo $userID;?>"/>
                        <input type="hidden" name="orgID" value="<?php echo $orgID;?>"/>
                        <button class="btn loginBTN">SAVE</button>
                    </form>

Open in new window

LVL 12
Nathan RileyFounderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
The code below should help you out:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
var rss_index=0;
$(function(){
	//this handles the clicking of the "Add" buttons
	$('form').on('click','.add-rss',function(){
					//extract the html from 'rss-template'
		var item = $('#rss-template').html()
					// then replace '[]' in the template with rss_index.  Both 
					// "title" and "link" are 'tied/related' to each other by
					// the same value in rss_index.  This way on the server
					// if you see $_POST['rssInfo'][0]['title'], you will know for
					// sure that its "related" link is $_POST['rssInfo'][0]['link']
					.split('[]').join('['+rss_index+']');

		//append the updated html markup into the DOM after the last "form-group"
		$(item).insertAfter(".form-group:last");
		
		//increment rss_index
		++rss_index;
	});
	
	// "auto-click" the first "Add" button, to insert the first RSS field
	$('input.add-rss:eq(0)').trigger('click');
});
</script>

</head>
<body>
<form class="col-md-12" action="submit.php" method="post" style="margin-top:20px;margin-bottom:20px;">

						<!-- 
						This is ignored by the browser.  It is here just as a "template".  
						I'm extracting its content dynamically and inserting it at the end 
						to insert a new RSS node set.
						-->
						<script id="rss-template" type="text/html">
                            <div class="form-group">
                                <label>RSS Feeds</label><div class="clearfix"></div>
                                <div class="col-md-6 noPadLeft">
                                    <input class="form-control" name="rssInfo[][title]" placeholder="Title"/>
                                </div>
                                <div class="col-md-6 noPadLeft">
                                    <input class="form-control" name="rssInfo[][link]" placeholder="Link"/>
                                </div>
								<input type="button" value="Remove" onclick="this.parentNode.parentNode.removeChild(this.parentNode)"/>
                                <div class="clearfix"></div>
								<hr />
                            </div>
						</script>

                        <div class="form-group">
                            <label><span class="accountHead">Custom Insights</span></label><br><br>
                            <input type="radio" name="insightsMode" value="0"> Disabled
                            <input type="radio" name="insigntsMode" value="1" checked=""> Enabled
							<input class="add-rss" type="button" name="add" value="Add"/>
                        </div>

                        <input type="hidden" name="submitType" value="20"/>
                        <input type="hidden" name="userID" value="<?php echo $userID;?>"/>
                        <input type="hidden" name="orgID" value="<?php echo $orgID;?>"/>
                        <button class="btn loginBTN">SAVE</button> <input class="add-rss" type="button" name="add" value="Add"/>
                    </form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.