3

Using Jquery 1.7.1

I have two divs

<div class="highlightStart"></div>
{page content here}
<div class="highlightEnd"></div> 

Those show up in the page source. But this jquery I added at the bottom of the page is not working:

<script type="text/javascript" id="makeHighlight">
   $(document).ready(function () {
     $("div.highlightStart").replaceWith("<section class='highlight'>");
     $("div.highlightEnd").replaceWith("</section>"); 
   });
</script>

No javascript errors showing in the browser console (Chrome). Just nothing gets replaced.

Conando
  • 219
  • 1
  • 4
  • 16

3 Answers3

3

First i want to site that you're a producing an incorrect structure of DOM. If your script will run it will looks like this:

<div class="highlightStart"><section></div>
{page content here}
<div class="highlightEnd"></section></div> 

and this is not a good structure if you want have:

<section>
{page content here}
</section>

Should be something like this:

Your DOM:

<div id="content">
{page content here}
</div>

And in your script:

$(document).ready(function () {
  content = $('#content').text();

  $('#content').html($('<section>').text(content));
});

Please see myfiddle for reference

Drixson Oseña
  • 3,631
  • 3
  • 23
  • 36
  • My understanding is that what you are describing would be for html() not for replaceWith(); html() replaces the content between tags; replaceWith replaces elements. Here is the StackOverflow post I got this from: http://stackoverflow.com/questions/730916/whats-the-difference-between-jquerys-replacewith-and-html – Conando Oct 22 '13 at 04:17
1

The replaceWith method expects entire elements, not tags. You'll need to wrap your page contents with a new element, then remove the two original divs.

Update: This might get you close:

$(document).ready(function () {
   $('.highlightStart').nextUntil('.highlightEnd').andSelf()
       .wrap('<section class="highlight"></section>');

    $('.highlightStart, .hightlightEnd').remove();
});

http://jsfiddle.net/isherwood/H36UE

Something's off a bit with this, but I'm out of time. Good luck.

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • Thanks for that Isherwood! In the JQuery documentation, is says that replaceWith can accept an HTML String (as well as an element, array or jquery). What I'm using seems to meet the definition of an HTML String: http://api.jquery.com/Types/#htmlString. – Conando Oct 22 '13 at 04:26
  • The issue seems to be that JQuery will autocomplete a tag into an element so you can not separately insert a start and end tag (it seems to ignore the end tag.) However, this works to just insert text as expected: http://jsfiddle.net/rqrxv/ – Conando Oct 22 '13 at 20:53
  • This is what I ended up using, closely following your example. The sample HTML shows more the structure of the tree in the client's site: http://jsfiddle.net/H36UE/1/. Thanks a lot for the help! – Conando Oct 24 '13 at 17:08
1

Based on help from isherwood, used this as the solution:

http://jsfiddle.net/H36UE/1/

With HTML tree like this:

<div><div><div class="highlightStart">highlightStart</div></div></div>
<div>Outside<div>Content to Highlight</div>More</div>
<div>second</div>
<div>third</div>
<div><div><div class="highlightEnd">highlightEnd</div></div></div>

This Javascript:

$(document).ready(function () {
  $('.highlightStart').parent().parent().replaceWith("<div class='highlightStart'>");
  $('.highlightEnd').parent().parent().replaceWith("<div class='highlightEnd'>");
  $('.highlightStart').nextUntil('.highlightEnd').andSelf().wrapAll("<section class='highlight'>");

 $('.highlightStart, .highlightEnd').remove();
});
Conando
  • 219
  • 1
  • 4
  • 16