views:

45

answers:

2

Is there a jquery plugin that enables replacing predefined phrases with links in a defined div tag. Forexample with an array of

"global warming" -> "global warming" "computer hardware" -> "computer hardware"

etc...

Also it will be used in a wordpress blog which jquery plugin already installed. Is there wordpress pluging that allows something like this?

A: 

You could adapt this plugin pretty easily, I bet: http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html

It's not terribly complicated, and the main difference is that it wraps the words in span tags while you'd want to wrap them in some sort of concocted anchor tag (where presumably the href has something to do with the words?).

Oh, and I have no idea whether/how this might work within Wordpress, but if it supports jQuery at all I don't see why it'd be a problem.

Pointy
A: 

None that I have heard of but it should be simple to write one:

(function($) {
    $.fn.phrasor = function(subst) {
        return this.each(function() {
            var txt = $(this).html();

            for ( key in subst )
                txt = txt.split(key).join(subst[key]);

            $(this).html(txt);                
        });
    }
})(jQuery);

EDIT I forgot to mention that you would have to pass an object to "phrasor" like so:

$('#mydiv').phrasor({'NY Times': '<a href="http://nytimes.com/"&gt;NY Times</a>'});

Did not test it thoroughly but I think that should do it. Cheers!

aefxx
Well, that *might* work, but he'd probably rather not find his strings by chance embedded in element "class" strings, or places like that. The plugin I mentioned goes through the DOM explicitly working on text nodes only.
Pointy
You're right. It would also look at his markup but then again I don't think our friend here is capable to alter the plugin himself. So, KISS is the way to go here, I think.
aefxx