Jump to content

Compare Old And New Values


s4surbhi2218

Recommended Posts

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 by s4surbhi2218
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by haku
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.