s4surbhi2218 Posted November 29, 2012 Share Posted November 29, 2012 (edited) Hi All, m writing a code to compare old and new values , i basically want two fields in html one containing the previous selected value when page loads and another the current value which user selects so that i can compare them in my js file and go on with other functionality. I am using this <?php $a = array(); for($i = 0;$i<=10;$i++) { $a[$i] = $i+1; } ?> <html> <body> <select name='name1' onchange = 'Javascript_Function(this.value)'> <option value = '-1'>--select--</option> <option value= '1'><?=$a['0']?></option> <option value= '2'><?=$a['1']?></option> <option value= '3'><?=$a['2']?></option> <option value= '4'><?=$a['3']?></option> </select> </body> </html> i can get the current value by this.value i need the previous value. please suggest. Many Thanks Edited November 29, 2012 by s4surbhi2218 Quote Link to comment Share on other sites More sharing options...
codefossa Posted November 29, 2012 Share Posted November 29, 2012 Quite sure this is a JS question. Anyways ... Are you using jQuery? If so, $.change() would be cleaner. Also, you would have $.data($(this), 'prev', $(this).val(); to set the previous when it changes, so you can do whatever with it before you set it to the new one. If not .. Plain JS can use addEventListener to keep it in JS completely, store it in a variable and do the same as before. The up-side to using $.data() is that you won't have to specify a variable for each list you do, or if there's only one. It kind'a does it for you. Basically an array where the key to the array for that element is the element itself. Quote Link to comment Share on other sites More sharing options...
s4surbhi2218 Posted November 29, 2012 Author Share Posted November 29, 2012 Quite sure this is a JS question. Anyways ... Are you using jQuery? If so, $.change() would be cleaner. Also, you would have $.data($(this), 'prev', $(this).val(); to set the previous when it changes, so you can do whatever with it before you set it to the new one. If not .. Plain JS can use addEventListener to keep it in JS completely, store it in a variable and do the same as before. The up-side to using $.data() is that you won't have to specify a variable for each list you do, or if there's only one. It kind'a does it for you. Basically an array where the key to the array for that element is the element itself. I am using simple js not jquery , need this with simple js / html only. Quote Link to comment Share on other sites More sharing options...
haku Posted November 29, 2012 Share Posted November 29, 2012 (edited) You have a few options. I'd set the value in the head of your document in the initial page load. For example, if you are using PHP, you can do something like this: <?php $original_value = some_function_that_returns_a_value(); ?> <script type="text/javascript"> var originalValue = <?php print $original_value; ?>; </script> Then inside any onload functions in your scripts, you will have access to the variable 'originalValue', which you can then compare with the current values. Edited November 29, 2012 by haku Quote Link to comment Share on other sites More sharing options...
s4surbhi2218 Posted November 29, 2012 Author Share Posted November 29, 2012 Thanks , Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.