clime
clime

Reputation: 8885

jquery: $.data() returns boolean instead of string

I have got the following html element:

<a href="javascript:void(0)" class="sortArrow down active" data-order_asc="false">
      <img src="/static/web/img/down-active.gif">
</a>

The problem is that

$('a.sortArrow.down.inactive').data('order_asc')

returns boolean false instead of string "false". There seems to be some kind of conversion that I don't really ask for. I would like string "false" to return.

Upvotes: 1

Views: 168

Answers (3)

Johnny Harlamert
Johnny Harlamert

Reputation: 271

According to the jQuery API "Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null)."

To retrieve the value's attribute as a string without any attempt to convert it, use the attr() method.

Upvotes: 1

Phil
Phil

Reputation: 165062

This is documented here - http://api.jquery.com/data/#data-html5.

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null).

If you want the attribute string value, use attr(), eg

$('a.sortArrow.down.inactive').attr('data-order_asc')

Upvotes: 2

Ankit Tyagi
Ankit Tyagi

Reputation: 2375

try this way: further reading : Attributes

$('a.sortArrow.down.inactive').attr('data-order_asc');

Upvotes: 1

Related Questions