0

If I have the following HTML

<Something attribute="" id="top">
    <Something attribute="child" id="child"></Something>
</Something>

I am trying to get a jquery object representing the second Something element. I am trying the following query (as suggested in this question Find all elements with a certain attribute value in jquery) in my browser console

$("Something[attribute='child']")

But I seem to be getting the same empty array each time, however the following works

$("Something[attribute='']")

This returns me an array of one element representing the first Something element

6
  • Something[attribute='child'] -> Selects something that has the attribute attribute='child' | Something [attribute='child'] -> selects something that has a child that has the attribute attribute='child'. Commented May 8, 2016 at 1:11
  • @IsmaelMiguel Wait I don't see the difference between those two.. Commented May 8, 2016 at 1:14
  • The space. That is the difference Commented May 8, 2016 at 1:14
  • I didn't have any space in my queries though... Commented May 8, 2016 at 1:15
  • Unable to reproduce jsfiddle.net/mowglisanu/rgzgw4bo Commented May 8, 2016 at 1:16

1 Answer 1

0

This will return a DOM array of divs which have id (attribute) "stack":

var stack = $('div[id=stack]');

var result = " ";

$(stack).each(function(index, element){
    result = element.id + result;
});

document.body.innerHTML = result
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

<div id="stack"></div>
<div id="stack"></div>
<div id="notstack"></div>
<div id="notstack"></div>

Sign up to request clarification or add additional context in comments.

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.