0

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');
1
  • create this as a snippet or fiddle. This will not reproduce your problem as far as I can tell. Please ensure that any posted code reproduces your problem. Commented Aug 17, 2017 at 22:48

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

17 Comments

.data can't change it?
.data will change the attached data, but not the the value of the attribute.
It looks like you can change it in the documnetaion
what's the diff between attached data and attribute value?
The attached data as the associated data with that element. It can be object/reference/array/anything javascript supports. It is not part of the DOM. When you talk about attributes - the values are strings.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.