jQuery: Place deep content between matching comments inside special tags

Using jQuery, how can I put the all the content including deep tags content that is between the comments containing "RED" and the comments containing "/RED" inside <em> tags?

In this example, the words: "Big" and "Fine" should be inside the <em> tags because they are between the comments.
http://jsbin.com/zedaxusapa/1/edit?html,js,output

The problem in the example above is that because the previous sibling is NOT the matching comment, everything before it fails to be wrapped with the <em> tag.
LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
http://jsbin.com/lukome/1/edit?html,js,output

$(function() {
  $("*").contents().each(function(i,e){
    if (e.nodeType===8) {
      //  console.log("|"+e.data+"|");
      if(e.data.indexOf("/RED")>-1) {
        //$(e.previousSibling).wrap("<em>");
        var t = e.previousSibling; 
        while (t.nodeType!==8) {
          if(t.nodeType) {
            switch (t.nodeType) {
              case 3:
                $(t).wrap("<em>");
                break;
              case 1:
                $(t).wrap("<em>");
                break;
            }
            t = t.parentNode.previousSibling;
          }
          else {
            t = t.previousSibling;
          }
        }
      }
    }

  });
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skijAuthor Commented:
That wraps them in DIFFERENT <em> tags.  How could they be wrapped in the same <em> tag?
0
RobOwner (Aidellio)Commented:
The issue is they're different nodes.. jQuery nor html see your comment as a start and end tag.  A comment is its own entity.

How much of an issue is it to be in the one <em>?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

skijAuthor Commented:
It is very important that they are inside the same <em> tag.  

The opening comment tag will always be: <!-- RED --> and the closing tag will always be <!-- /RED --> and everything between must be wrapped inside the same <em> tag.  Thanks for you help with this!
0
RobOwner (Aidellio)Commented:
Ok, let me asleep on it, my brain is fried!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.