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");
}
attr()does not return an array, or find the element that has that attribute to get the value$('div[context]').attr('context')but you should probably consider putting an id or class on it to avoid the attribute selectordata-- so in this case it's best practice to usedata-context="james". Also jQuery has specific support fordata-*attributes: api.jquery.com/data