tags:

views:

36

answers:

1

Hello all I have an JS array like the following:

var associativeArray = [];
associativeArray["1"] = "First";
associativeArray["2"] = "Second";
associativeArray["3"] = "Third";

Now I need to use jquery to use the words like First, Second from the array and auto link them to a certain URL on the page. For example:

Lets say the current page has the following line: Third comes after Second and Second comes after First. So here all these keywords like First, Second and Third should be converted to First. The trick is that each word should be linked only once and if a word is already linked then it should not be linked again.

Hope I have been able to explain what exactly is needed. Please help me out. Many many thanks in advance for your kind help!

+1  A: 

jQuery does not support text-based selector, so you'll have to parse the text and find these words with old plain javascript. Once you have tagged the required words, you may use jQuery to add or remove links or whatever you like.

There is a piece of code which scans the text for specific words and (in this case) highlights them. You could easily modify it to your needs.

Here it is: JavaScript text higlighting jQuery plugin

Patonza
This is a nice plugin. The only thing noticed was that it highlighted the term even if it was part of a larger word. Should be a config flag you can set to avoid that.
Marco