How do I get all the LI UL elements ID value and place them in a JavaScript array?

TheBlackBenzKid

I have to embed some tracking code on my site. So I have a list of LI elements with an ID value that I want to place inside an array of the snippet. They should be numeric like, 123, 456, etc inside an object. I want to do it in pure JavaScript.

This is my code I have tried. My HTML:

<ul id="itemGrid">  
    <li class="item" id="1080">  product code </li>
    <li class="item" id="1487">  product code </li>    
    <li class="item" id="1488">  product code </li>
    ...
</ul>

This is the JavaScript code

// Get all LI items and get the ID of them in the object viewList
var catId = document.getElementById('itemGrid').getElementsByTagName('li');

window.criteo_q = window.criteo_q || [];
    window.criteo_q.push(
        // SHOULD BE LIKE THIS
        // { event: "viewList", item: ["First item id", "Second item id", "Third item id"] }
        // My actual code
        { event: "viewList", item: [ catId[].id ] }
);
Tushar

You can use querySelectorAll to select all the matching elements passed as selector.

The selector '#itemGrid li[id]' will select all the <li> elements inside #itemGrid element having id attribute on it.

The querySelectorAll returns a collection of HTML elements. Iterate over this collection to get the individual element id.

var lis = document.querySelectorAll('#itemGrid li[id]');

var arr = [];
for (var i = 0; i < lis.length; i++) {
  arr.push(+lis[i].id);
}

console.log(arr);
document.write('<pre>' + JSON.stringify(arr, 0, 4) + '</pre>');
<ul id="itemGrid">
  <li class="item" id="1080">1080</li>
  <li class="item" id="1487">1487</li>
  <li class="item" id="1488">1488</li>
</ul>
<hr />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to get "title" attribute of all visible <li> elements in specefic <ul> using jquery, collect theme in an array and display them using alert();

From Dev

how to get "title" attribute of all visible <li> elements in specefic <ul> using jquery, collect theme in an array and display them using alert();

From Dev

How do I find all the <li> having data-et-id="A" from <ul> using selectors?

From Dev

How To Get Child Elements Of tag 'li' In The 'ul' In javascript

From Dev

Get all elements with similar id array in javascript

From Dev

How do I get an array with all elements with the same key?

From Dev

How do I add a string to all existing elements in array in javascript?

From Dev

Get li value from ul filled by JavaScript

From Dev

How do I force all ul elements to stay in one line?

From Dev

How to get previous LI in multi-level UL/LI elements

From Dev

how to get UL LI value in jquery?

From Dev

how to get all li elements has class show-skill in ul

From Dev

How do I remove the border of all <li> elements on the last row?

From Dev

Add css class to all li parent elements in the ul with javascript

From Dev

How can I get all array elements, where the value only occurs once in the array?

From Dev

Get all list items from various ul elements - merge all of them into a new ul

From Dev

How to get the id value of an html <li> element added to an html <ul> element using prepend() method in jQuery ?

From Dev

How to get the id value of an html <li> element added to an html <ul> element using prepend() method in jQuery ?

From Dev

How to do 2 column in ul tag with two elements in li

From Dev

How do wrap these groups of li elements in ul containers?

From Dev

How to get a list of the <li> elements in an <ul> with Selenium using Python?

From Dev

How do I parse ul li a tree into a nested array using simpleXML?

From Dev

Get the value of active li in ul

From Dev

how do i get rid of the zero that shows up after all my elements in my array are listed?

From Dev

How do I nest an array of items dividing them by their first value?

From Dev

How do I nest an array of items dividing them by their first value?

From Dev

how can I do an addition of all price elements from a json array in javascript

From Dev

How do I collapse an array of arrays into an array of all the elements?

From Dev

How to get the selected ID of a UL / LI Box by Clicking on a Button - Jquery

Related Related

  1. 1

    how to get "title" attribute of all visible <li> elements in specefic <ul> using jquery, collect theme in an array and display them using alert();

  2. 2

    how to get "title" attribute of all visible <li> elements in specefic <ul> using jquery, collect theme in an array and display them using alert();

  3. 3

    How do I find all the <li> having data-et-id="A" from <ul> using selectors?

  4. 4

    How To Get Child Elements Of tag 'li' In The 'ul' In javascript

  5. 5

    Get all elements with similar id array in javascript

  6. 6

    How do I get an array with all elements with the same key?

  7. 7

    How do I add a string to all existing elements in array in javascript?

  8. 8

    Get li value from ul filled by JavaScript

  9. 9

    How do I force all ul elements to stay in one line?

  10. 10

    How to get previous LI in multi-level UL/LI elements

  11. 11

    how to get UL LI value in jquery?

  12. 12

    how to get all li elements has class show-skill in ul

  13. 13

    How do I remove the border of all <li> elements on the last row?

  14. 14

    Add css class to all li parent elements in the ul with javascript

  15. 15

    How can I get all array elements, where the value only occurs once in the array?

  16. 16

    Get all list items from various ul elements - merge all of them into a new ul

  17. 17

    How to get the id value of an html <li> element added to an html <ul> element using prepend() method in jQuery ?

  18. 18

    How to get the id value of an html <li> element added to an html <ul> element using prepend() method in jQuery ?

  19. 19

    How to do 2 column in ul tag with two elements in li

  20. 20

    How do wrap these groups of li elements in ul containers?

  21. 21

    How to get a list of the <li> elements in an <ul> with Selenium using Python?

  22. 22

    How do I parse ul li a tree into a nested array using simpleXML?

  23. 23

    Get the value of active li in ul

  24. 24

    how do i get rid of the zero that shows up after all my elements in my array are listed?

  25. 25

    How do I nest an array of items dividing them by their first value?

  26. 26

    How do I nest an array of items dividing them by their first value?

  27. 27

    how can I do an addition of all price elements from a json array in javascript

  28. 28

    How do I collapse an array of arrays into an array of all the elements?

  29. 29

    How to get the selected ID of a UL / LI Box by Clicking on a Button - Jquery

HotTag

Archive