admin管理员组文章数量:1328037
I have a page where users can create tags (much like here in stackoverflow), which are then sent(POST) to the back end to be stored in a database. The user can make tags but also remove them before finally hitting Submit.
In the DOM the tags are generated along with an 'x' button. The 'x' button removes the element from the DOM, but the trouble es when removing from the array. The closest I could get to a solution was this question, however I couldn't get it to quite work for me.
Here's the codepen
Here's the javascript (i'm using JQuery)
window.tag_array = [];
$( "#addtag" ).click(function() {
var tag = $("#input-tag").val();
//if tag is empty
if(!$('#input-tag').val()) {
alert("can't be empty");
} else {
//put tag.val into an array
tag_array.push(tag);
//add to DOM
$( "#tagsbox" )
.append( "<div class='displaytag'><i>"+tag+"</i><input type='hidden' class='tag' value="+tag+"><button onClick='return false;' class='removetag'>x</button></div>" );
//reset value in text area to null
$("#input-tag").val("");
//remove tag onclick
$('.removetag').click(function() {
$(this).parent().remove(); //remove tag from DOM
//splice from array
tag_array.splice( this, 1 ); //<--HERE IS PROBLEM (i think)
});
} //end else
alert(tag_array); //check array
});
The end result is the splice takes out too many array items.
I have also tried
tag_array.splice(tag_array.indexOf(tag),1);
to a similar result.
Please help! Thanks in advance
I have a page where users can create tags (much like here in stackoverflow), which are then sent(POST) to the back end to be stored in a database. The user can make tags but also remove them before finally hitting Submit.
In the DOM the tags are generated along with an 'x' button. The 'x' button removes the element from the DOM, but the trouble es when removing from the array. The closest I could get to a solution was this question, however I couldn't get it to quite work for me.
Here's the codepen
Here's the javascript (i'm using JQuery)
window.tag_array = [];
$( "#addtag" ).click(function() {
var tag = $("#input-tag").val();
//if tag is empty
if(!$('#input-tag').val()) {
alert("can't be empty");
} else {
//put tag.val into an array
tag_array.push(tag);
//add to DOM
$( "#tagsbox" )
.append( "<div class='displaytag'><i>"+tag+"</i><input type='hidden' class='tag' value="+tag+"><button onClick='return false;' class='removetag'>x</button></div>" );
//reset value in text area to null
$("#input-tag").val("");
//remove tag onclick
$('.removetag').click(function() {
$(this).parent().remove(); //remove tag from DOM
//splice from array
tag_array.splice( this, 1 ); //<--HERE IS PROBLEM (i think)
});
} //end else
alert(tag_array); //check array
});
The end result is the splice takes out too many array items.
I have also tried
tag_array.splice(tag_array.indexOf(tag),1);
to a similar result.
Please help! Thanks in advance
Share Improve this question edited May 23, 2017 at 11:53 CommunityBot 11 silver badge asked Sep 8, 2014 at 16:20 bjurtownbjurtown 1734 silver badges13 bronze badges2 Answers
Reset to default 5You should probably use something like .indexOf()
to get an index of the element and then splice an array:
tag_array.splice(tag_array.indexOf(elm),1);
Working demo
The splice
part is OK. The problem is that you're adding a click callback to .removetag
too many times.
Everytime you append a new element, you are adding another click
event to every .removetag
item that is already on the page.
$('.removetag').click(function()
This way, whenever you click on one element, all the others were assign to fire the click callback too.
Solution
Instead, when creating the tag, set the click event only to the last added .removetag
element:
$('.removetag').last().click(function()
Updated CODEPEN
本文标签: jqueryJavascriptremove specific element from dynamically created arrayStack Overflow
版权声明:本文标题:jquery - Javascript - remove specific element from dynamically created array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742237853a2438405.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论