Skip to content

Instantly share code, notes, and snippets.

@tahsingungordu
Created March 27, 2016 18:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tahsingungordu/2dfa360c14e4b9e5128c to your computer and use it in GitHub Desktop.
Save tahsingungordu/2dfa360c14e4b9e5128c to your computer and use it in GitHub Desktop.
Add and delete rows with jQuery
$(window).load(function(){
$(function() {
var scntDiv = $('#listValue');
var i = $('#listValue').size() + 1;
$(document).on('click','#addItem', function() {
$('<span><input name="inputName[]" type="text" /> <a id="delItem" href="#">Delete Row</a><br /></span>').appendTo(scntDiv);
i++;
return false;
});
$(document).on('click','#delItem', function() {
if( i > 2 ) {
$(this).parents('span').remove();
i--;
}
return false;
});
});
});
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="custom-js.js"></script>
<ul class="addDelList">
<li id="listValue">
<input name="inputName[]" type="text">
<a href="#" id="addItem">Add Row</a><br />
</li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment