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
4
votes
2 answers

jquery data do not match dom attribute

I have an weird issue with jquery data function. Here is the fiddle As you can see I update the active data but I cannot see the dom data-active attribute value change, although I re-query the active data, It writes the changed value. $.data() do…
px5x2
  • 1,495
  • 2
  • 14
  • 29
4
votes
1 answer

How to keep data value intact?

I have this basic HTML:
First page
and my jQuery: var number = $('#div1').data('pagenumber') $('#div2').append(number); Fiddle: http://jsfiddle.net/JabUS/ Why is the text…
user2358524
4
votes
1 answer

Storing Data in the DOM - Custom Attributes or .data()

Possible Duplicate: jQuery Data vs Attr? I'm working on a project where I plan on storing small amounts of data in the DOM. I'm specifically using it to attach a numerical value to a DOM element to be easily accessed elsewhere. Is it better to…
streetlight
  • 5,968
  • 13
  • 62
  • 101
3
votes
2 answers

How to share jQuery .data() data through iframe's?

I'm trying to share jQuery data from one or more iframe'd html pages with their parent html document. What I need is an inter-iframe communication and if possible (highly desireble) to share/exchange .data() i.e. the $.cache of both jQuery objects…
Azder
  • 4,698
  • 7
  • 37
  • 57
3
votes
7 answers

Modify value of attribute

How to modify the value of data-tooltip? I would like to do that with one line of code. Here is the element:
  • gsamaras
    • 71,951
    • 46
    • 188
    • 305
  • 3
    votes
    2 answers

    Encode php array to keyed JSON array for use in javascript?

    I need to create a keyed JSON array to pass a set of images that are queried via PHP to a vegas background slideshow which expects the images to be in an array like so: $("#example, body").vegas({ slides: [ { src: "/img/slide1.jpg" }, …
    waffl
    • 5,179
    • 10
    • 73
    • 123
    3
    votes
    3 answers

    jQuery.data only saves data from the last Element

    i am trying to use jQuery.data() and save an Object to my HTML-Elements. Everytime i add an list-Element to my unordered List it only saves the last object to the specific li-Element. Every other li-Elements saved data gets thrown away! I've built…
    B. Kemmer
    • 1,517
    • 1
    • 14
    • 32
    3
    votes
    0 answers

    How to check if an element has an event or not?

    How can I check if an element has an event or not with jQuery? I would like to clone or store that event for later use. html,
    edit delete
    Run
    • 54,938
    • 169
    • 450
    • 748
    3
    votes
    2 answers

    Retrieving multiple data-attributes and setting as text of a child element

    Bottom Line: I'm having trouble retrieving the value of multiple data-attributes and inserting them as the text of a child element. Details: I've got a table with each tr "bucket" containing a particular number of three kinds of shapes (spheres,…
    Marcatectura
    • 1,721
    • 5
    • 30
    • 49
    3
    votes
    2 answers

    Why use $.data() instead of directly setting an object via JavaScript?

    I often read it's bad practice, because it's hard to maintain, but doing: document.getElementsByTagName("h1")[0].foo = {"key":"value"}; compared to using the recommended jQuery alterantive: $.data($(document.getElementsByTagName("h1")[0]), "foo",…
    frequent
    • 27,643
    • 59
    • 181
    • 333
    3
    votes
    3 answers

    Optimise jQuery filter for unique elements

    I have a collection of elements, and whilst each has a unique String identifier, I can't safely use it's value as the elements ID as I can't guarantee that it'll only contain valid characters. As a result, I'm storing the identifiers in each of the…
    Ian Clark
    • 9,237
    • 4
    • 32
    • 49
    2
    votes
    3 answers

    Add some properties to a

    I want to add some properties to a
    element. All of the below works except for the .data(). I cannot see it appear in Firefox/Firebug. $('#menu-container') .css('background-image','url("'+big_image+'")') .addClass('click_2') …
    Mustapha George
    • 2,497
    • 9
    • 47
    • 79
    2
    votes
    1 answer

    Getting different results from attribute.dataset and jquery .data() method

    I was getting unexpected results and when I debugged into the problem, I found that I was not getting right data-attribute value by Jquery .data() method. It was pretty clear that value was not right and when I changed my code to…
    2
    votes
    2 answers

    jQuery and .data()

    I'm writing a jQuery plugin and I need to keep objects through my plugin method calls. So I tried to use .data() as recommended here : http://docs.jquery.com/Plugins/Authoring but I'm unable to retrieve my stored objects, here my code : (function($)…
    Fabien Engels
    • 363
    • 1
    • 5
    • 15
    2
    votes
    2 answers

    Creating native dom element, with data that jQuery's data() will use

    I'd like to create a DOM element, natively, attaching data to it. and than, later, create a jQuery element of it, setting (automatically) these data to be available through the data() function. var x = document.createElement("div"); x. ???? // add…
    yossi
    • 3,090
    • 7
    • 45
    • 65
    1
    2
    3
    9 10