Bug Tracker

Ticket #4132 (closed bug: invalid)

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#4132 closed bug (invalid)

Data store hash object bug

Reported by: dave_smith Owned by:
Priority: minor Milestone: 1.3.2
Component: data Version: 1.3.1
Keywords: Cc:
Blocked by: Blocking:


The desired output of the following code is that a#other gives 'other' and the rest of the links give 'value'.

Instead, all links give 'other'.

If the same is done with a string instead of a hash object, then the desired output produced.

$(document).ready(function() {

Store a hash on all links. $('a').data('object_store', {prop: 'value'});

Get the stored hash from one of the links. var object_store = $('a#other').data('object_store');

Alter the hash. object_store.prop = 'other';

Store the hash in the one link. $('a#other').data('object_store', object_store);

$('a').click(function() { Display the output. alert($(this).data('object_store').prop); return false; });


Change History

comment:1 Changed 7 years ago by dmethvin

comment:1 Changed 7 years ago by dmethvin

  • Resolution set to invalid
  • Status changed from new to closed

All the elements are sharing the object that you initially set. I updated the documentation for jQuery.data to clarify this.


If you want a subset of the objects to have a different value, you could use jQuery.extend to create a new object with updated values:

  $.extend({}, $('a#other').data('object_store'), {prop: other})

Or if you want each element to have a unique object, do this:

  $(this).data('object_store', {prop: something});

comment:2 Changed 7 years ago by dave_smith

comment:2 Changed 7 years ago by dave_smith

Thanks dmethvin, your explanation and code examples are much appreciated.

Note: See TracTickets for help on using tickets.