views:

31

answers:

1

I need to pass 300 or less strings from anchors of tags on page to jquery's:

var availableTags = [
"my tag",
"my tag1"
];

Taglist

Server gives me a taglist only of a certain format were the number of tags is 300 (or less). Every anchor is put in the div. That div has a static class "tagItem":

<div class="tagItem">
 <a href="/search/my some first tag/" rel="nofollow" class="eTag eTagGr123">
  my tag
 </a>
</div>
<div class="tagItem">
 <a href="/search/my some first tag/" rel="nofollow" class="eTag eTagGr435">
  my tag1
 </a>
</div>

What is need to be done is to take the anchor string (my some tag) and put it dynamically into the available tags.

Is it possible at all? And if so, how can it be properly done. Note: I can't make changes to the taglist output format due to the restrictions.

A: 

After some testing, here's a complete example:

<html>
  <head>
    <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
$(document).ready(function() {
    var availableTags = [];
    $('div.tagItem a').each(function() {
        var text = $(this).text();
        availableTags.push(text);
    });

    // at this point, availableTags is:
    // ['Tag One', 'Another Tag', 'Tag Three', 'Last Tag']

    $('#tags').autocomplete({
        source: availableTags
    });
});
    </script>
  </head>
  <body>

    <div class="tagItem"><a href="/search/tag one">Tag One</a></div>
    <div class="tagItem"><a href="/search/tag two/">Another Tag</a></div>
    <div class="tagItem"><a href="/search/tag three/">Tag Three</a></div>
    <div class="tagItem"><a href="/search/tag four/">Last Tag</a></div>

  </body>
</html>
Rob Hruska
doesn't work. is your script passing string in this format - "some tag"? Also there are no tags I can see betweenn [ ]; when i open html code of generated page
dessol
Sorry, but I'm not following what you're saying. My example here is simply an idea that might work for you, but you may have to adjust it to meet your specific needs. Can you clarify your comments a bit?
Rob Hruska
i mean this text wich is passed to availableTags must be enclosed in quotes and after every word there must be a comma. Like this
dessol
["my some frist tag","my second tag string from an anchor"];
dessol
Well, `availableTags` is just an array. The `push()` function adds each found tag to the array. Unless you have some sort of server-side scripting you're talking about that generates the array before the page is served?
Rob Hruska
I have that html code with anchors at the bottom of my page. I need text from that anchors to be placed with correct syntax to the array so when I type in - it shows those tags... Don't know how to explain more clearly...
dessol
not the href of an anchor but the text it contains (my some first tag)
dessol
Ok, let's try a different approach here - when you use the code in my example, what happens? You said it "doesn't work"; can you elaborate on how/why? Do you see any errors in the console?
Rob Hruska
I saw one minor issue with my script, I've updated it. I'm testing it out right now.
Rob Hruska
No errors, nothing happens... There are no changes on the output page (i see no tags passed to availableTags)
dessol
I just updated my answer to have a complete example. Give it a try.
Rob Hruska
That is just perfect! Worked excellent. Many thanks
dessol
Just one more question. What if that taglist is on some other page? How can I get that anchor string?
dessol
That's a little bit tougher. It's possible you could use http://api.jquery.com/jQuery.get/ - however, that'd be a bit of a dicey solution if implemented incorrectly.
Rob Hruska
Thanks for the tip. It worked better with .load() and .keypress()
dessol