10

I can use the jquery .data attribute and an alert confirms that it worked, but I don't see data attribute in page using firebug.

$('#something').data('foo', 52);        
alert($('#something').data('foo'));     
Mustapha George
  • 2,497
  • 9
  • 47
  • 79
  • How exactly are you expecting to "see" it in Firebug? `.data()` does not use HTML attributes. – Matt Ball Mar 27 '12 at 16:04
  • 2
    That's because the `.data()` isn't actually stored on the DOM element, or its jQuery object. It can read `data-` attributes, but won't write to them. – Blazemonger Mar 27 '12 at 16:06
  • 1
    possible duplicate of [How does jQuery .data() work?](http://stackoverflow.com/questions/2764619/how-does-jquery-data-work) – j08691 Mar 27 '12 at 16:06

2 Answers2

15

The information put into .data(...) is not a visible DOM attribute.

You can view an object's data by doing console.log($('#something').data());

Demo: http://jsfiddle.net/maniator/pQybU/

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 1
    Hi Neal, one thing, I installed FireQuery to check out data attributes, I found that even when they are programatically changed mid-way (as evidenced by alerts showing different values before and after,) FireQuery keeps on showing the same initial value. Is it a known bug? – SexyBeast Mar 16 '13 at 22:11
  • @Cupidvogel how do you view it using firequery? ive been trying to figure it out. (would save alot of `console.log`ing) – Math chiller Jun 25 '14 at 12:19
4

jQuery doesn't actually add a data attribute as such to the element and so there is nothing in the DOM that firebug can display.

As Neal points out you can log the data to console to verify that it is in fact there.

Alternatively you can install a Firebug extension such as FireQuery (http://firequery.binaryage.com/) to directly display jQuery data in Firebug itself.

benz001
  • 2,158
  • 1
  • 21
  • 23
  • 2
    FireQuery is not working since Firebug 2.0 (middle of 2014). Check [FireQuery reviews](https://addons.mozilla.org/en-us/firefox/addon/firequery/reviews/). – s3m3n Jun 12 '15 at 11:27