Diego Eann
Diego Eann

Reputation: 47

Wrap single word inline jQuery

I'm working on an inline squirrel parse for syntax highlighting and I had been trying to use .code:contains and .wrap with a span to select an inline word. Example:

<div class="code">
    function Entity::MoveTo(newx,newy,newz)
{
    x = newx;
    y = newy;
    z = newz;
}
</div>

With this line of jQuery:

$(".code:contains('function')").wrap('<span class="function"></span>');

This turns the whole div red, rather that just the word 'function'. Any suggestions? Thanks in advance!

Upvotes: 1

Views: 179

Answers (1)

williamsandonz
williamsandonz

Reputation: 16460

Something like this? :

var text = $(".code").text();
text  = text.replace('function','<span class="function">function</span>');
$(".code").html(text);

Upvotes: 2

Related Questions