4

I've an array of HTML DOM objects:

// Console output

(3) […]
0: <div data-selector="5">​
1: <div data-selector="9">​
2: <div data-selector="2">​
3: <div data-selector="6">​
4: <div data-selector="13">​
length: 5
<prototype>: Array []

How can I find (and return) the HTML object having data-selector equal to 9 (i.e.<div data-selector="9">​) using JavaScript?

3
  • How does this array get logged? Do you have control over the code that logs the array? Commented Apr 5, 2019 at 9:56
  • Do you get this array from an outside source, or construct on your own (by selecting elements). If latter is the case, then its better to utilize the attribute selector and select only the desired element. That will be efficient. Commented Apr 5, 2019 at 10:09
  • I've no control over the code that gets the array. I get this array from an outside source. Commented Apr 5, 2019 at 11:33

5 Answers 5

5

This would solve your problem.

array.find(item => item.dataset.selector === '9');

Explanation:

Javascript allows you to use dataset to access data attributes in html. That is, they have a pattern such as data-*

So, if you have a html file that looks like this:

<html>
  <div id="root">
    <div data-selector="5"></div>
    <div data-selector="9"></div>
    <div data-selector="2"></div>
    <div data-selector="6"></div>
    <div data-selector="13"></div>
  </div>
</html>

You can get the div with id of root in your javascript file:

const root = document.querySelector('#root');

// get array of div elements inside root
const tagsArray = [...root.children];

// finally, retrieve the specific element you need
const selectedItem = tagsArray.find(elem => elem.dataset.selector === '9');

// A simple check
console.log(selectedItem); // => <div data-selector="9"></div>
Sign up to request clarification or add additional context in comments.

Comments

2

You can use querySelectorAll function and specify the attribute ie: data-selector and assign the value which is 9.

Note: The querySelectorAll function returns an array of elements.

const x = document.querySelectorAll("[data-selector='9']");
console.log(x[0].innerHTML);
<p data-selector="9">Test</p>

1 Comment

I get this array from an outside source and do not have control over the code that gets it.
0

You can do that Using getAttribute() method

var myArray // Let this be your array
var len = myArray.length
// Implement this logic
for(i=0;i<len;i++) {
    if(myArray[i].getAttribute('data-selector') == '9') {
        // Return myArray[i] or do what ever you want to do
    }
}

1 Comment

se my answer. Thank you.
0

Use Array.find:

// Array of HTML DOM objects:
var myArray = ['<div data-selector="5">', '...', '<div data-selector="9">'];

var found = myArray.find(function(element) {
  return element.getAttribute('data-selector') === '9';
});

Based on getAttribute() by @vaibhav kumar

Comments

-1

You could do it like this (where array is your array of HTML elements):

array.forEach(element => {
  if(element.getAttribute("data-selector") == "9") doSomething(element);
});

If the array's data-selector attribute is equal to "9", the function doSomething() gets executed and passed the element.

1 Comment

It doesn't return the desired element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.