jQuery objects html5 custom attributes DATA is being cached.
In my application I have a form with a field that has a changing custom data attribute, and this specific behavior is imperative for the functionally of the form.
There is an input field with some default custom attribute:
<input type="text" name="xxx" data-test="4">
for $('input').data()
the result would be { test="4" }
$('input').attr('data-test','5')
for $('input').data()
the result would STILL be { test="4" }
How can I always make sure to get all the REAL custom attributes, there can be more than one on an element, using the $.data()
function? I have tried the $.removeData()
before each fetch, but it cleans all the data completely from the element so it isn't accessible any longer.
You need to do this: $('input').data( 'test' , 5 )
If you call .attr( 'test' , 5 )
you will be setting an attribute, and you are affecting it like this <input type='text' data-test='4' test='5' />
As a note, you can get specific data attributes like this: var test = $('input').data('test');
edit
For removing a specific data attribute, you can do this: jQuery.removeData( $( 'input' ) , "test" );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With