0

I'm using expressionengine as a CMS, after the page is loaded, I need to find 'arbesthealth' and make it ar*best*health, What would be the best way to do this? Jquery after document ready? can it be done with CSS?

note: I can't do it on the cms side because EE doesn't allow tags inside the title fields so it needs to be done on output. I guess I could do it with a substr in php, but I'm just curious to the other ways that this would be possible.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
nick
  • 59
  • 8

2 Answers2

1

The simple solution would be to manipulate innerHTML (directly, or through jQuery's html function), but that will destroy and recreate all of the elements, which isn't ideal — not least because any event handlers attached to them will get removed.

But a simple recursive function processing text nodes and inserting strong elements as necessary isn't difficult. My answer to this other SO question shows how to do that, walking through the text nodes and using Text#splitText to split them up and insert elements. Sounds like a lot more work than it is.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Wrap a tag inside the word like this:

$('.content').wrapInTag({
    words: ['best'],
    tag: '<span>'
});

See FIDDLE

I added in .content but you can add it wherever the cms has the container


*UPDATE*

As mentioned this will effect all so lets try this method:

html().replace

SPECIFIC FIDDLE

Riskbreaker
  • 4,621
  • 1
  • 23
  • 31
  • 1
    That will blow away any event handlers assigned to the elements by jQuery or `addEventListener` prior to running it. So it would have to be first thing, before anything hooked up any handlers. – T.J. Crowder Sep 23 '13 at 17:01
  • I really like this, but how could I alter this so that it doesn't find the word "best" unless it is in the occurance of arbesthealth? – nick Sep 23 '13 at 17:04
  • @T.J.Crowder has a point...hence why you need to know what class is this word/logo/ title specifically since you could have a paragraph with the word 'best'..given you might have it on a paragraph. – Riskbreaker Sep 23 '13 at 17:12