Questions tagged [jquery-data]

jQuery `.data` can store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

jQuery's .data Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.

Example

One short example of use is to set data with `

$("body").data("foo", "bar");

which can later be returned by calling

$("body").data("foo");`, 

which will return "bar".

You can also use .data to return the value of a custom data- attribute. For example, if you have the HTML element

<div id="myDiv" data-info="My Info"></div>`

you can call

$("#myDiv").data('info');

which will return "My Info". Note that .data will not edit or add these attributes, only read.

Resource

140 questions
46
votes
3 answers

jquery get HTML 5 Data Attributes with hyphens and Case Sensitivity

How to get data attributes using jquery .data()? In which case html5 data-* attributes converts to lowercase and camelcase? What are all the main rules to follow while using custom attributes to store data?
Kiran
  • 20,167
  • 11
  • 67
  • 99
39
votes
6 answers

HTML5 data-* attribute type casting strings and numbers

Why is the value of data-value="2.0" cast to a String and the value of data-value="2.5" cast to a Number? I can handle this fine within my function. I'm just trying to understand a little more about how Javascript handles Numbers and Strings. This…
Matt
  • 549
  • 4
  • 11
37
votes
3 answers

Where is jQuery.data() stored?

Where does jQuery store the values of the data() that it sets to DOM objects? Is there some kind of variable like jQuery.dataDb or something, maybe even something private? Is there any way to gain access to this object?
qwertymk
  • 34,200
  • 28
  • 121
  • 184
31
votes
5 answers

How to check if an element is droppable, draggable or other 'ble'?

I've got a bunch of elements with jQuery. Some are draggable, some are droppable and some are both. How can I detect if an element is draggable or droppable?
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
26
votes
3 answers

How to remove value from FormData

Here is a way to append file to FormData : var data = new FormData(); jQuery.each($('#file')[0].files, function(i, file) { data.append('file-'+i, file); }); is it possible to do as below ? data[i].remove();??? or data[i] =…
talkhabi
  • 2,669
  • 3
  • 20
  • 29
25
votes
4 answers

How to preserve the case of a data attribute?

The html object:
The code: var $o = $("div"); $.each($o.data(),function(k,v){ console.log(k); //writes 'myattribute' instead of 'myAttribute' }); How do I preserve the case of the attribute?
Control Freak
  • 12,965
  • 30
  • 94
  • 145
17
votes
3 answers

Setting a value dynamically for data attributes using jquery

I use data attributes extensively for managing data in client side events. Is it possible to assign value dynamically to a data attribute using javascript or jquery?
  • Abishek
    • 11,191
    • 19
    • 72
    • 111
  • 12
    votes
    4 answers

    what does jQuery data() function do

    I have not found what is the use of jquery function data(). Can anyone give me some example of how it can be used?
    John
    12
    votes
    3 answers

    How to hide jQuery datatables

    How do I to hide jQuery datatables, when I want to toggle the datatable's visibility? The problem is if I write hide statement it only hides the rows, not the headers and footer. How can I hide datatable fully? For hiding, I used the below code: …
    Rajaram Shelar
    • 7,537
    • 24
    • 66
    • 107
    7
    votes
    10 answers

    Getting a JSON file using jQuery without a web server

    I had a coding interview quiz for front-end working with JSON and whatnot. I submitted my file but I'd just like to learn what I was missing. And one of the reqs was Should not require a web server, and should be able to run offline.. I used jQuery…
    test
    • 17,706
    • 64
    • 171
    • 244
    7
    votes
    5 answers

    Group and count HTML elements by data attribute in jQuery

    I'm trying to group each instance of a data attribute into a list from the number of occurrences of each attribute in a page, using jQuery. Similar to grouping categories or tags in a news section. For example, I would like to create something like…
    mmmoustache
    • 2,273
    • 6
    • 41
    • 62
    6
    votes
    1 answer

    Prevent converting string containing an E and numbers to number

    We have this 'strange' situation where some product codes, for example 11E6, which are stored in data attributes (ex data-prodcode) are getting converted to 11000000, when retrieved inside jquery click function. Something like this:
    user1398498
    • 377
    • 3
    • 11
    5
    votes
    2 answers

    Data attribute changes not detected with jQuery

    I am changing the data-demo attribute of a div element and when I want to check it with in another event, it always shows me the initial value of it instead of the current value. This is the code I am using: $('#showValue').click(function(){ …
    Alvaro
    • 40,778
    • 30
    • 164
    • 336
    4
    votes
    3 answers

    jQuery $.data() conditional statement

    I'm using the html5 "data" attribute on a element, and I want to assign the attribute value to a variable only if it exists and if it's not empty: var xxx = $(this).data('what') ? $(this).data('what') : 'default_value'; but it doesn't work. I always…
    Alex
    • 66,732
    • 177
    • 439
    • 641
    4
    votes
    3 answers

    What's the reason for storing data with .data() instead of using plain variables?

    Let's say I have to remember the initial height of certain element. A common practice I see is to save this value with $.data on that element. I fail to understand the benefits of this. Why not simply keep a simple variable with that value, or an…
    thelolcat
    • 10,995
    • 21
    • 60
    • 102
    1
    2 3
    9 10