jQuery tag-it, how to execute post when item removed
-
28-10-2019 - |
Question
I'm using the jQuery Tag-It script, can be viewed here:
http://levycarneiro.com/projects/tag-it/example.html
The script originally does not come with the option to send posts of the added tags or remove tags that the user removes.
I successfully added the post request to a php script so that when someone adds a tag, it inserts it in the DB.
The problem is, when someone clicks the 'x' button to remove one of the tags, I can't seem to find a way to get the actual tag value..
Solution
Here is the code modified so you can have access to the tag that gets removed..
In the click
handler (of the x)
if (e.target.tagName == 'A') {
// Removes a tag when the little 'x' is clicked.
// Event is binded to the UL, otherwise a new tag (LI > A) wouldn't have this event attached to it.
var tag = $(e.target).parent();
//console.log( tag.children('input').val() ); // this line extracts the tag value
tag.remove();
}
and in the keypress
handler use
if (tag_input.val() == "") {
// When backspace is pressed, the last tag is deleted.
var tag = $(el).children(".tagit-choice:last");
// console.log( tag.children('input').val() ); // this line extracts the tag value
tag.remove();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow