Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] need help for my swaping function
- From: patcito at gmail.com (Patrick Aljord)
- Subject: [Javascript] need help for my swaping function
- Date: Thu Sep 21 21:39:32 2006
i wrote a function that adds tags to a textbox and remove them if
there are in already like in delicious:
function addTag(ele) {
var thisTag = ele.innerHTML;
var taglist = document.getElementById('tags');
var tags = taglist.value.split(', ');
// if the tag is in the list, we remove it
if (tags.contains(thisTag)) {
tags = tags.remove(thisTag);
ele.className = 'unselected';
// else we add it
} else {
tags.splice(0, 0, thisTag);
ele.className = 'selected';
}
taglist.value = tags.join(', ');
document.getElementById('tags').focus();
}
Array.prototype.contains = function (ele) {
for (var i = 0; i < this.length; i++) {
if (this[i] == ele) {
return true;
}
}
return false;
};
Array.prototype.remove = function (ele) {
var arr = new Array();
var count = 0;
for (var i = 0; i < this.length; i++) {
if (this[i] != ele) {
arr[count] = this[i];
count++;
}
}
return arr;
};
and in my code I have:
<p><label for="tags">Tags</label><br/>
<input id="tags" name="tags" size="40" type="text" /></p>
<span id="tag1" name="tag1" onclick="addTag(this)">
tag1
</span>
<span id="tag2" name="tag2" onclick="addTag(this)">
tag2
</span>
it works well, when I click on a tag it gets displayed in the textbox
and when I click on the tag again it removes it correctly. But if I
write a tag in the textbox that also feature in one of the tag span,
then if I click in the tag span it won't remove it. I will add it
again to the list as if the script didn't check tags added by hand.
Any idea what's wrong?
thanx in advance
Pat
- Prev by Date: [Javascript] Image PreLoad
- Next by Date: [Javascript] how to disable right click mouse option in IE browser
- Previous by thread: [Javascript] event handlers
- Next by thread: [Javascript] how to disable right click mouse option in IE browser
- Index(es):