4

Let's say for example that I have a webpage.

<ul>
  <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
  <li>Aliquam tincidunt mauris eu risus.</li>
  <li>Vestibulum auctor dapibus neque.</li>
</ul>

I want to find all instances of the word 'Lorem' and manipulate them in two ways:

  1. Wrap it with <mark> HTML5 element
  2. Run .addClass('look-at-me'); on its parent element.

So the resulting HTML would be

<ul>
  <li class="look-at-me"><mark>Lorem</mark> ipsum dolor sit amet, consectetuer adipiscing elit.</li>
  <li>Aliquam tincidunt mauris eu risus.</li>
  <li>Vestibulum auctor dapibus neque.</li>
</ul>

I've read all the comments on Highlight a word with jQuery and I've been playing around with the JS code from highlight: JavaScript text higlighting jQuery plugin but both of these deal with ONLY highlighting the word in context. I manipulated the code to wrap the word using <mark> but I'm not skilled enough with JS to achieve my #2 goal of highlighting the parent container. I'm eager to see your helpful suggestions. Thanks!

EDIT: SOLVED! http://jsfiddle.net/GB8zP/1/

Community
  • 1
  • 1
Daniel Fowler
  • 385
  • 7
  • 21
  • 1
    Can you post an example on jsfiddle.net where you got the individual word wrapping to work? – Explosion Pills Mar 07 '13 at 17:12
  • So basically you want to find a word, and append divs around it, am i correct? edit: sorry maybe not append div around it but append a class to the element/
  • in your case
  • – NodeDad Mar 07 '13 at 17:15
  • Sure thing. http://jsfiddle.net/Bkbsw/ – Daniel Fowler Mar 07 '13 at 17:16
  • Here's a similar setup I provided in an answer a while back: http://stackoverflow.com/a/9240077/451969 It could be retrofitted to add a class to the parent's easy enough. Edit: Like this: http://jsfiddle.net/JaN75/40/ – Jared Farrish Mar 07 '13 at 17:22
  • Am I oversimplifying the issue here? http://jsfiddle.net/Bkbsw/2/ – Jared Farrish Mar 07 '13 at 17:34
  • Given that you already have the word wrapping working, why not just run `$('mark').parent().addClass('look-at-me');` after you run your highlight function? – dnagirl Mar 07 '13 at 17:34
  • 1
    Looks like you just updated your fiddle to use my example. Am I missing something? – j08691 Mar 07 '13 at 17:44