ahmad valipour
ahmad valipour

Reputation: 303

Jquery selector base data attribute with single quote or two quotes

I have an html element with multiple data attributes :

 <div class="passenger-information clearfix" data-index="0"   data-class="passenger-info-wrapper" data-age-classify="Adult"></div>

I will use jquery selectors as below:

1)$('[data-age-classify="Adult"]')
2)$("[data-age-classify='َAdult']")
3)$("[data-class='passenger-info-wrapper']")

The problem is that the first selector despite the second and third ones doesn't return anything.

Upvotes: 0

Views: 696

Answers (3)

All of them work, but the second one you dont need the '

so it looks like: $("[data-age-classify=Adult]"

console.log($('[data-age-classify="Adult"]').html())
console.log($("[data-age-classify=Adult]").html())
console.log($("[data-class='passenger-info-wrapper']").html())
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="passenger-information clearfix" data-index="0"   data-class="passenger-info-wrapper" data-age-classify="Adult">s</div>

Upvotes: 1

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

It is working as expected. Please check if there are any console errors on your browser or if jquery library not imported properly.

alert($('[data-age-classify="Adult"]'));
alert($("[data-age-classify='َAdult']"));
alert($("[data-class='passenger-info-wrapper']"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="passenger-information clearfix" data-index="0"   data-class="passenger-info-wrapper" data-age-classify="Adult"></div>

Upvotes: 1

KAD
KAD

Reputation: 11102

Attributes are placed inside the start tag, and consist of a name and a value, separated by an = character. The attribute value can remain unquoted if it doesn’t contain spaces or any of " ' ` = < or >. Otherwise, it has to be quoted using either single or double quotes. The value, along with the = character, can be omitted altogether if the value is the empty string.

All the cases should work in your example. Check this article for more understanding of the topic: https://mathiasbynens.be/notes/unquoted-attribute-values

Upvotes: 0

Related Questions