Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating HTML data attribute correctly but changes not being displayed on page

Tags:

html

jquery

I have this HTML:

<input type="text" id="query" data-source="whatever">

I have some jQuery that changes the data attribute successfully i.e. "whatever" changes to "test"

$(function() {
    $('#query').data('source', 'test');
    console.log($('#query').data());
});

but if I inspect the page using Chrome, the data attribute has not been updated in the element.. I can print it in console, but I can't inspect the new value! Very confusion. Any ideas?

here is the fiddle

like image 321
ale Avatar asked Feb 02 '12 15:02

ale


1 Answers

The data isn't stored on the element (use attr or prop for that). Instead jQuery maintains it in $.cache.

like image 129
Caleb Avatar answered Nov 09 '22 22:11

Caleb