chuckd
chuckd

Reputation: 14530

JQuery .data() not fetching values after I change them

I have a data attribute called registeredteacherid. For some reason the values aren't getting fetched correctly after I update them. Here is a general idea of what I'm doing.

var registeredTeacherId = $(eventPanel).data('registeredteacherid');
// do some work...then
$(eventPanel).data('registeredteacherid', response.teacherId);
// at this point it sets the new value in the IE debugger window
// do some more work
// then I fetch the value again and it still shows the old value
var registeredTeacherId = $(eventPanel).data('registeredteacherid');

Upvotes: 0

Views: 25

Answers (1)

Dekel
Dekel

Reputation: 62556

The data function will not change the value of your data-* attribute, but it will give you access to these values.

You can always change the values and get the new data, and if you want to change the content of the data-* attribute you can use the attr function:

console.log($('div').data('content'));
console.log($('div').attr('data-content'));
$('div').data('content', 'some new content');
console.log($('div').data('content'));
console.log($('div').attr('data-content'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-content="this is the content">Text</div>

Upvotes: 1

Related Questions