Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get ALL id's of children elements

How do I get an array or something similar with ALL the id's of elements to a certain div?

Let's say I have someting like this:

<div id="parent-div">
    <div id="div-no-1"></div>
    <div id="div-no-2"></div>
    <div id="div-no-3"></div>
    <div id="div-no-4"></div>
</div>

I would then like an array that look's something like this

parent-div [
    0: "div-no-1",
    1: "div-no-2",
    2: "div-no-3",
    3: "div-no-3"
];


I've tried this...

$("#parent-div > div").attr("id");

...but it only gives me the first childs id, e.g. div-no-1. I want ALL of them

like image 961
wenzzzel Avatar asked Feb 16 '19 21:02

wenzzzel


People also ask

How do you get all elements for kids?

To get all child nodes of an element, you can use the childNodes property. This property returns a collection of a node's child nodes, as a NodeList object. By default, the nodes in the collection are sorted by their appearance in the source code. You can use a numerical index (start from 0) to access individual nodes.

How do you get children of elements in selenium?

We can locate child nodes of web elements with Selenium webdriver. First of all we need to identify the parent element with help of any of the locators like id, class, name, xpath or css. Then we have to identify the children with the findElements(By. xpath()) method.

How do you get children of children in jquery?

The children() method returns all direct children of the selected element. The DOM tree: This method only traverse a single level down the DOM tree. To traverse down multiple levels (to return grandchildren or other descendants), use the find() method.

How to get the ID of every child div element in JavaScript?

You can use the children property of getElementById () method in JavaScript to get or extract unique ids of all the DIV elements inside a DIV element. The name of the property itself says it all. I am sharing a simple example here, which shows how to get the IDs of every child DIV element using the children property.

How to get the parent element by its ID in HTML?

Call the querySelector method on the parent element passing it the id as a parameter. For example, parent.querySelector ('#first') gets the child with id first. Here is the HTML for the examples in the article. And here is the related JavaScript code. We used the document.querySelector method to get the parent element by its id.

How to get the first child element of an element?

The HTML DOM firstElementChild property can return the first child element of a specific element that we provide. It doesn’t matter how many child elements are there, it will always return the first one.

How to determine the number of child elements in HTML collection?

The index starts at 0. Tip: You can use the length property of the HTMLCollection object to determine the number of child elements, then you can loop through all children and extract the info you want.


Video Answer


2 Answers

An alternative to Jack Bashford's solution using $.map:

const divIds = $.map($('#parent-div > div'), div => div.id);

console.log(divIds);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div id="parent-div">
  <div id="div-no-1"></div>
  <div id="div-no-2"></div>
  <div id="div-no-3"></div>
  <div id="div-no-4"></div>
</div>

Or, using .map and .get:

const divIds = $('#parent-div > div').map((i, div) => div.id).get();

console.log(divIds);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div id="parent-div">
  <div id="div-no-1"></div>
  <div id="div-no-2"></div>
  <div id="div-no-3"></div>
  <div id="div-no-4"></div>
</div>
like image 175
Jeto Avatar answered Sep 24 '22 11:09

Jeto


Use jQuery's each and push the id to the array:

var parentDiv = [];
$("#parent-div > div").each((index, elem) => {
  parentDiv.push(elem.id);
});

console.log(parentDiv);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div id="parent-div">
    <div id="div-no-1"></div>
    <div id="div-no-2"></div>
    <div id="div-no-3"></div>
    <div id="div-no-4"></div>
</div>
like image 29
Jack Bashford Avatar answered Sep 22 '22 11:09

Jack Bashford