Created
May 22, 2016 06:28
-
-
Save hbhargava7/63e1507b4c1fadfdb2131d0751f76341 to your computer and use it in GitHub Desktop.
Add additional (removable) fields to HTML form when user tabs out of last field (jQuery).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Created by Hersh Bhargava of H2 Micro (www.h2micro.com) */ | |
//Keep track of dynamically added text inputs in 'i' | |
var i = $('#items p').size() + 1; | |
//Keyup function to add text input on tab release from last field | |
$(document).on('keyup', 'input[type="text"]:last', function (e) { | |
//check if the released key was the tab key | |
if (e.which == 9) { | |
//append additional fields to items div | |
var targetDiv = $('#items'); | |
//include the desired html here, with a #remove id on the element that should remove the fields. | |
$('YOUR_HTML_CONTENT').appendTo(targetDiv); | |
i++; | |
return false; | |
} | |
}); | |
//Remove set on inputs closest to clicked remove button | |
$(document).on('click', '#remove', function () { | |
if (i > 1) { | |
$(this).closest('YOUR_CONTAINING_ELEMENT').remove(); | |
i--; | |
} | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment