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

Link to comment
https://forums.phpfreaks.com/topic/271331-compare-old-and-new-values/
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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.