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 6335925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:57:05+00:00 2026-05-24T18:57:05+00:00

I’m trying to fill an array with all direct child elements of a div.

  • 0

I’m trying to fill an array with all direct child elements of a div.
example:

<div>
    <span></span>
    <a></a>
</div>

should result in an array containing span and a

How Would I achieve this?

I tried var elements = $('.thediv').children(); but that doesn’t seem to work.

Also how Would I then use that information for this kind of function?:

$(“element1″,”element2″,”element3”) depending on the result of the array?

Really thank you for your help! I am kind of lost with this thing

Note: I’m using zepto.js which has a similar syntax to jquery but misses a lot of functions so that might be a problem but I am also happy with solutions using jquery syntax because it should work pretty well 🙂 https://github.com/madrobby/zepto

  • 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-24T18:57:06+00:00Added an answer on May 24, 2026 at 6:57 pm

    To get the tags into the array, you could easily use, with jQuery (though I’m unfamiliar with zepto):

    var elementsArray = [];
    
    $('div').children().each(
        function(i){
            elementsArray.push(this.tagName.toLowerCase());
        });
    

    JS Fiddle demo.

    And to use them, you can try:

    for(i=0;i<elementsArray.length;i++){
        $('div').find(elementsArray[i]).css('color','red');
    }
    

    JS Fiddle demo.

    Although this simply uses the tagName of each element, so if there is more than one a within the relevant element all elements matching the selector, effectively $('div').find('a') in this case, will be targeted by the selector.

    The above caution seems to be discounted through use of a more complicated selector, and behaviour, and allows for each element to be iterated over one at a time, though I can’t help but feel it’s a little more complex than it needs to be:

    var elementsArray = [];
    
    $('div').children().each(
        function(i){
            elementsArray.push(this.tagName.toLowerCase());
        });
    
    for(i=0;i<elementsArray.length;i++){
        $('div')
            .find(elementsArray[i])
            .not('.edited')
            .eq(0).css('color','red')
            .addClass('edited');
    }
    

    JS Fiddle demo.

    Sigh, I’m an idiot. The final iteration of this is below, which reduces the complexity somewhat, and allows for proper iteration over each element according to their position in the array:

    var elementsArray = [];
    
    $('div').children().each(
        function(i){
            elementsArray.push(this.tagName.toLowerCase());
        });
    
    for(i=0;i<elementsArray.length;i++){
        $('div')
            .children()
            .eq(i)
            .css('color','red');
    }
    

    JS Fiddle demo.

    Having said the above, though, if you want to “target all elements within,” why not simply target those elements with:

    $('div').children();
    

    Which will select, and target, each direct child of the div element without first holding them in, or having to hold them in, a user-created array variable.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.