Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:16:42+00:00 2026-05-30T23:16:42+00:00

I’m just getting started with Javascript and am struggling to find a way to:

  • 0

I’m just getting started with Javascript and am struggling to find a way to:

  1. Find one or more consecutive elements of the same type, e.g. <p>…</p> or <p>…</p><p>…</p>
  2. Surround each group with a new element, e.g. <dd><p>…</p></dd> or <dd><p>…</p><p>…</p></dd>

I’d also like to limit the script so that it applies only to a particular part of the page, e.g. <div id="relevantbit"></div>, but perhaps that counts as a separate question.

Thanks in advance for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:16:43+00:00Added an answer on May 30, 2026 at 11:16 pm

    Here’s is a native javascript function that will wrap consecutive tags at the same level in whatever type of tag you want it wrapped in. It even works recursively so it will find them to any level. As written, when detecting consecutive tags, it ignores non-elements such as text nodes or comment nodes, but it could easily be modified if that was not the desired behavior (that detail wasn’t specified).

    You can see a working demo here: http://jsfiddle.net/jfriend00/Bp97p/.

    And here’s the code:

    function wrapConsecutive(parent, desiredTagName, wrapTag) {
    
        desiredTagName = desiredTagName.toUpperCase();
        if (typeof parent === "string") {
            parent = document.getElementById(parent);
        }
    
        function wrapNodes(nodeBegin, nodeEnd) {
            // create and insert the wrap node
            var wrapNode = document.createElement(wrapTag);
            nodeBegin.parentNode.insertBefore(wrapNode, nodeBegin);
            // now move the matched nodes into the wrap node
            var node = nodeBegin, next;
            while (node) {
                next = node.nextSibling;
                wrapNode.appendChild(node);
                if (node === nodeEnd) {
                    break;
                }
                node = next;
            }
        }
    
        function wrapChildren(parent) {
            var next = parent.firstChild;
            var firstInSeries = null;
            var lastInSeries;
            while (next) {
                // only look at element nodes
                if (next.nodeType === 1) {
                    wrapChildren(next);
                    if (next.tagName === desiredTagName) {
                        // found a matching tagName
                        // if we don't have a series yet, start one
                        // if we do have a series, just keep going
                        if (!firstInSeries) {
                            firstInSeries = next;
                        }
                        lastInSeries = next;
                    } else {
                        // did not find a matching tagName
                        // if we have a series, then end the series and process it
                        // if we didn't have a series yet, then keep looking for one
                        if (firstInSeries) {
                            // wrap from firstInSeries to next.previousSibling
                            wrapNodes(firstInSeries, lastInSeries);
                            firstInSeries = null;
                        }
                    }
                }
                next = next.nextSibling;
            }
            if (firstInSeries) {
                wrapNodes(firstInSeries, lastInSeries);
            }
        }
        wrapChildren(parent);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm making a simple page using Google Maps API 3. My first. One marker
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.