Javascript Error: DOM Exception 8 using jQuery and an Array
I am getting this error:
Uncaught Error: NOT_FOUND_ERR: DOM Exception 8
Here is my code (please suggest anything to m开发者_开发问答ake it more efficient / cleaner):
Basically this is a button thats adding it's ID to an array called 'keywords':
$('.add').live('click', function() {
if($(this).text() == "+Add") {
console.log("add triggered");
$(this).stop().animate({backgroundColor:'#999d92'}, 300);
$(this).html("-Rem").fadeIn('fast');
keywords.push($(this).attr("id"));
$("#response").append(keywords);
}
else {
$(this).stop().animate({backgroundColor:'#cc6633'}, 300);
$(this).html("+Add").fadeIn('fast');
var index = keywords.indexOf($(this).attr("id"));
keywords.splice(index, index+1);
$("#response").append(keywords);
}
});
What I want to happen is when "+ADD" is pushed the id attribute is added to the array, and when -REM is pushed then remove that id from the keywords.
Any advice would really be a help. When I just append $(this).attr("id") to the response div it does print correctly. I also tried surrounding it with a "String()" function (perhaps it was a reference to the resource and not the actual string?)
Thanks a head of time!
keywords.splice(index, index+1);
The second argument to splice
is a number of items to remove, so that should probably be just 1
.
$("#response").append(keywords);
But keywords
is an array of strings? jQuery documents no such interface for append
.
Do you want something like:?
$("#response").text(keywords.join(', '));
精彩评论