Rejoanul Alam
Rejoanul Alam

Reputation: 5398

jQuery variable not updating when dynamically changed

jQuery variables like following is working nicely when i reload page. But Once I change the data-agent_id attribute dynamically, after that "live" click not changing the "assigned_agent" variable as changed value. I have tried to declare the variable global still not working.

var assigned_agent = parseInt($(this).data('agent_id'));

Any help will highly appreciate.Thanks

Upvotes: 0

Views: 330

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074028

data isn't for accessing data-* attributes. data manages a cache of data associated with the element which is only initialized from data-* attributes when you first access it; after that, it's totally and completely disconnected from them.

To interact with data-* attributes, use attr, not data:

var assigned_agent = parseInt($(this).attr('data-agent_id')); // Or whatever the attribute name actually is

Alternately, of course, always use data, not attr, to both get and set, and understand that the attribute in the DOM will get out of sync with it.

Upvotes: 3

Related Questions