Topic: need help with a tag swapping function

this is the JS function I use:
function addTag(tagText,that) {
    var repetida = false;
    var tags = new Array();
    var etiquetas = new String();
    var temp = new String();

    etiquetas = document.getElementById('tags');
    tags = etiquetas.value.split(/\s*,\s*/);

    for (var i=0; i<tags.length; i++) {
        if (tags[i] == tagText) {
            tags[i] = "";
            repetida = true;
        }
    }
    if (!repetida) {
        tags[tags.length] = tagText;
    that.className = 'selected';
    }
    if (repetida) {
    that.className = '';
    }


    temp = "";
    for (var i=0; i<tags.length; i++) {
        if (tags[i] != '') {
            temp = temp + "," + tags[i];
        }
    }
    etiquetas.value = temp.substring(1);
   
    return true;
       
}


here is my _form.rhtml code:

<p><label for="tags">Tags</label><br/>
<%= text_field_tag 'tags',@bookmark.tag_names.join(",") ,:size=>"40"%></p>
<%= 'related tags:' %>
<% for tag in tags %>
&nbsp;<a href="javascript:void(0)" name="<%= tag %>" <% if @bookmark.tagged_with?(tag)==true %>class="selected" <% end %> onclick="addTag('<%= tag %>',this)" title="Click to tag this entry with '%2C'"><%= tag %></a>
<%= 'all tags:' %>
<% for tag in @alltags %>
&nbsp;<a href="javascript:void(0)" name="<%= tag %>" <% if @bookmark.tagged_with?(tag)==true %>class="selected" <% end %> onclick="addTag('<%= tag %>',this)" title="Click to tag this entry with '%2C'"><%= tag %></a>

What this code do is display my tags delicious like. When a user click on tag it appears in the text box and when the user click back it is taken back of the text box. Also when the user click on a tag, its css class is switch to 'selected' to highlight it. The problem is that if the same tag is present in 'related tags' and in 'all tags', only the one I'm clicking on will get highlighted. Any idea how to highlight them both?

thanx in advance


Pat

Re: need help with a tag swapping function

If you include prototype you could do something like:

matches = $$('[name=' + tag + ']');
for (var i = 0; i < matches.length; i++) {
  matches[i].className = 'selected';
}

There's probably a better way though, as I'm not that great with javascript...

Railscasts - Free Ruby on Rails Screencasts

Re: need help with a tag swapping function

thanx a lot