26

Fallback is irrelevant. No libraries, please.

We have an dom object reference, we'll call obj. It's actually an event.target.

We have a node list, we'll call nodes, which we've gotten with querySelectorAll and a variable selector.

nodes may have 1 or many elements, and each each of those elements may have children.

We need to determine if obj is one of those node elements, or children elements of those node elements. We're looking for "native" browser functionality here, we can totes write our own for loop and accomplish this, we are looking for alternatives.

Something like:

nodes.contains(obj) OR nodes.indexof(obj)

Solutions involving other methods of retrieving the node list to match against are acceptable, but I have no idea what those could be.

6 Answers 6

24

If <=IE11 is not a concern then I think the cleanest is to use Array.from

Array.from(nodes).find(node => node.isEqualNode(nodeToFind));
Sign up to request clarification or add additional context in comments.

1 Comment

15

I'm not sure if this will search beyond the first level of the NodeList, but you can use this expression recursively to traverse it and check if the element 'obj' is in the NodeList 'nodes'.

[].indexOf.call(nodes, obj)

Comments

7

I don't think there's a built-in DOM method for that. You'd need to recursively traverse your NodeList, and check for equality with your element. Another option is to use Element.querySelectorAll on each first-level elements from your NodeList (looking for your element's id, for example). I'm not sure how (inn)efficient that would be, though.

1 Comment

Yeah this is our current school of thought as well. Well, sort of... the obj is from an event.target, so we would recurs through the parents, and match against our variable selector using matchesSelector. We have been unable to find a built-in method as well, but wanted to pick some great minds to see if we've missed.
7

I did something like this:

Array.prototype.find.call(style.childNodes, function(child) {
  if(child.textContent.includes(drawer.id)) {
    console.log(child);
  }
});

Seems to work. Then child is another html node, which you can manipulate however you like.

Comments

4

You can convert the NodeList to an Array to then use Array.includes() to determine whether it includes the targetNode among its entries

[...nodes].includes(targetNode)

1 Comment

Please don't post code-only answers. The main audience, future readers, will be grateful to see explained why this answers the question instead of having to infer it from the code. Also, since this is an old question, please explain how it complements all other answers.
0

In addition to Dominic's answer as function:

function nodelist_contains (nodelist, obj)
{
  if (-1 < Array.from (nodelist).indexOf (obj))
    return true;
  
  return false;
} 

Comments

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.