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.

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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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:

$(function() {
    if (e.nodeType===8) {
      //  console.log("|"+e.data+"|");
      if(e.data.indexOf("/RED")>-1) {
        var t = e.previousSibling; 
        while (t.nodeType!==8) {
          if(t.nodeType) {
            switch (t.nodeType) {
              case 3:
              case 1:
            t = t.parentNode.previousSibling;
          else {
            t = t.previousSibling;


Open in new window

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?
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>?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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!
RobOwner (Aidellio)Commented:
Ok, let me asleep on it, my brain is fried!
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

From novice to tech pro — start learning today.