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

  • SEARCH
  • Home
  • 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 7883437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:31:55+00:00 2026-06-03T04:31:55+00:00

If I have a list of similar elements, such that: <outer-container> <element attribute=x />

  • 0

If I have a list of similar elements, such that:

<outer-container>
    <element attribute="x" />
    <element attribute="x" />
    .
    .
    .
    <element attribute="y" />
    <element attribute="z" />
</outer-container>

How can I wrap the elements with attribute="x" so they are within their own container?

<outer-container>
    <inner-container>
        <element attribute="x" />
        <element attribute="x" />
    </inner-container>
    .
    .
    .
</outer-container>
  • 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-06-03T04:31:56+00:00Added an answer on June 3, 2026 at 4:31 am

    I changed your HTML to make it easier to test.

    <div>
        <input attribute="x" />
        <input attribute="x" />
        <p>divider</p>
        <input attribute="y" />
        <input attribute="z" />
    </div>
    

    Assuming you want all of the elements of that kind in one container, positioned where the first one was found, you can do this.

       // invoked like this
    wrapAll('input[attribute=x]', 'span');
    

       // implemented like this
    function wrapAll(selector, wrapper) {
        if (typeof wrapper === 'string')
            wrapper = document.createElement(wrapper);
        else if (!wrapper || !wrapper.nodeType)
            throw "Illegal 'wrapper' argument."
    
        var els = document.querySelectorAll(selector);
    
        els[0].parentNode.insertBefore(wrapper, els[0]);
    
        for(var i = 0, len = els.length; i < len; i++)
            wrapper.appendChild(els[i]);
    }
    

    http://jsfiddle.net/5z2uA/4/


    If you wanted different consecutive groups to be wrapped together, you can do this.

       // invoked like this
    wrapAdjacent('input[attribute=x]', 'span');
    

       // implemented like this
    function wrapAdjacent(selector, wrapper) {
        if (typeof wrapper === 'string')
            wrapper = document.createElement(wrapper);
        else if (!wrapper || !wrapper.nodeType)
            throw "Illegal 'wrapper' argument."
    
        var els = document.querySelectorAll(selector);
    
        for (var i = 0, len = els.length; i < len; ) {
            var this_wrapper = wrapper.cloneNode(false);
            els[i].parentNode.insertBefore(this_wrapper, els[i]);
            do {
                this_wrapper.appendChild(els[i]);
                i++;
            }
            while (nextEl(this_wrapper) === els[i])       
        }
    }
    function nextEl(el) {
        while (el && (el = el.nextSibling) && el.nodeType !== 1) {}
        return el;
    }
    

    http://jsfiddle.net/5z2uA/5/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that similar questions have been asked, but they don't quite fit what
Let's say you have a list of similar sequences, such as a a a
I have a list, which may contain elements that will compare as equal. I
I have a list of strings similar to this list: tags = ('apples', 'apricots',
I have code similar to this: public List<string> Expected = new List<string>(); public int
I have an entity similar to the below: public class Entity { public List<DateItem>
i have list of rows that user select and i want to delete them,
I have list of words. I type in a word misspelled. Can I query
I have a std::set and I need to erase similar adjacent elements: DnaSet::const_iterator next
I have a list of n strings (names of people) that I want to

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.