0

Below is a sample structure from which i'm trying to get the specific value of custom attribute

<div id="123"></div>
<div id="456"></div>
<div context="james"></div>

Below is how i'm trying to fetch, but it always returns false.

if ( $('div').attr('context') == 'james' ) {
    alert("yes");
} else {
    alert("no");
}
3
  • 1
    The first div does not have that attribute. attr() does not return an array, or find the element that has that attribute to get the value Commented Nov 14, 2018 at 22:50
  • You could do $('div[context]').attr('context') but you should probably consider putting an id or class on it to avoid the attribute selector Commented Nov 14, 2018 at 22:51
  • 1
    btw, your html is not valid unless your custom attribute starts with data- - so in this case it's best practice to use data-context="james". Also jQuery has specific support for data-* attributes: api.jquery.com/data Commented Nov 14, 2018 at 22:59

2 Answers 2

3

The call to $('div').attr('context') will only grab the first div found in the DOM and check it's value. Since it doesn't have that attribute you get false. Instead you will want to iterate over all your div's and check each one. For example:

var found = false;
$('div').each(function( ) {
  if($(this).attr('context') === 'james') found = true;
});
if(found) alert("yes");
else alert("no")

You could also use .filter:

if( $('div').filter(function(){ return $(this).attr('context') === 'james' }).length )
    alert("yes");
else 
    alert("no");

Note: If you used data-context="james" you would use the .data() method rather than .attr().

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

1 Comment

Awesome answer, I love the alternative that uses .filter() because it's an easy one-liner :) might want to suggest OP to use strict equality === instead.
0

Simply let jQuery do the filtering for you. Also, this can be done with plain old vanilla javascript pretty easily:

// Select by attribute
var theDiv = $("div[context='james']");

// If we have something here...
if(theDiv.length){
  console.log(theDiv.text() );
} else {
  console.log("Nope, not found.");
}

// We can use this same selector with plain javascript...
let theSameDiv = document.querySelector("div[context='james']");

// In this event, however, we access the text differently.
if(theSameDiv){
  console.log("Found without jQuery!", theSameDiv.textContent);
} else {
  console.log("Rats...");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="123">the first div</div>
<div id="456">div the second</div>
<div context="james">I'm 'Enry the Eighth, I Am!</div>

2 Comments

The issue here is, while you can do this, attribute selectors effectively perform a DOM scan. The browser does not index elements by all their attributes, so when you search by an attribute, it literally has to step though every single node in the context to see if it matches. This is why for performance reasons it is better to use a tagName/class/id selector, or if you are forced to use an attribute only selector, to scope down the context upon which you are performing it on so as to reduce the number of nodes it has to evaluate against.
And you're absolutely right. I was simplifying, as I had a very limited number of nodes. Changed the code to include the div tagName, in order to limit that number of nodes. Very good point, and well worth noting. Thank you!

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.