Skip to content

Instantly share code, notes, and snippets.

@taboularasa
Created March 14, 2009 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taboularasa/79135 to your computer and use it in GitHub Desktop.
Save taboularasa/79135 to your computer and use it in GitHub Desktop.
add and delete form elements with jQuery, demo lives here: http://mohdshaiful.wordpress.com/2007/05/31/form-elements-generation-using-jquery/
<script type=”text/javascript” src=”jquery-latest.pack.js”></script>
<script type=”text/javascript” src=”jquery.highlightFade.js”></script>
function addFormField() {
var id = document.getElementById(”id”).value;
$(”#divTxt”).append(”<p id=’row” + id + “‘><label for=’txt” + id + “‘>Field ” + id + ” <input type=’text’ size=’20′ name=’txt[]‘ id=’txt” + id + “‘> <a href=’#’ onClick=’removeFormField(\”#row” + id + “\”); return false;’>Remove</a><p>”);
$(’#row’ + id).highlightFade({
speed:1000
});
id = (id - 1) + 2;
document.getElementById(”id”).value = id;
}
function removeFormField(id) {
$(id).remove();
}
<p><a href="#" onClick="addFormField(); return false;">Add</a></p>
<form action=”#” method=”get” id=”form1″>
<input type=”hidden” id=”id” value=”1″>
<div id=”divTxt”></div>
<p><input type=”submit” value=”Submit” name=”submit”>
<input type=”reset” value=”Reset” name=”reset”></p>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment