Shruti
Shruti

Reputation: 21

Jquery returns old value for an element on refresh

There is an html as follows:

<input id="currency_factor" type="text" style="display:none;" value="20"/>

And there is a jquery function:

$(document).ready(function(){
     var currency_factor = $('#currency_factor').val()
     alert(currency_factor);
 }

Upon refresh the html line shows on the browser as:

<input id="currency_factor" type="text" style="display:none;" value="0"/>

But jquery returns 20.

Why is this happening?

Upvotes: 0

Views: 656

Answers (3)

Eben Roux
Eben Roux

Reputation: 13256

Do you perhaps have more than one element with an id of currency_factor. The first one's value will probably be returned.

Another possibility is that the value is changed after the $(document).ready() function has executed. Try running $('#currency_factor').val() in the browser console after load. If it returns 0 then it was changed after the page load.

Upvotes: 0

brk
brk

Reputation: 50326

Try by first initializing it and then setting it

$(document).ready(function(){
     var currency_factor = 0;
     currency_factor = $('#currency_factor').val()
     alert(currency_factor);
 }

Upvotes: 0

jdigital
jdigital

Reputation: 12286

Try adding this to the header of your html file to disable caching:

<meta http-equiv="Cache-Control" content="no-store" />

There are actually a number of cache control tags that may or may not be needed, depending on the browsers being used. Here's another posting on StackOverflow that lists some of them: Using <meta> tags to turn off caching in all browsers?

Upvotes: 1

Related Questions