1

I am having a problem, when ever user selects from dropdown I need to check if there are changes that the user made before I allow the user to fully go to the next thing he/she should fill-out.

Is there a way to set the dropdown to it's previous value if there are changes made?

<select id="dropdown" >
    <option value="1">option 1</option>
    <option value="2">option 2</option>
    <option value="3">option 3</option>
    <option value="4">option 4</option>
</select>
Amit
  • 3,251
  • 3
  • 20
  • 31
user3462803
  • 170
  • 1
  • 2
  • 10
  • Why don't you disable the select when those changes are made. – Milind Anantwar Apr 10 '14 at 09:27
  • possible duplicate of http://stackoverflow.com/questions/5426387/jquery-prevent-change-for-select answer here http://jsfiddle.net/qk2Pc/ – 4dgaurav Apr 10 '14 at 09:29
  • You might be interested in [this SO post discussing the use of the `disabled` attribute](http://stackoverflow.com/questions/368813/html-form-readonly-select-tag-input). there is one catch (which the answers address) as disabled selections aren't included by default in the form parameters sent to the server. – collapsar Apr 10 '14 at 09:30

2 Answers2

1

Try this,

var prevVal=$('#dropdown').val();
$('#dropdown').on('click','option',function(){
   if(prevVal==this.value){
      alert('no change');   
   } else{
       prevVal = this.value;
       alert('value changed');       
   }       
});

Live Demo

Updated use click event on dropdown like,

var prevVal=$('#dropdown').val();
$('#dropdown').on('click',function(){
   if(prevVal==this.value){
       console.log('no change');   
   } else{
       prevVal = this.value;
       console.log('value changed');       
   }       
});

Updated demo

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0
var previous = $('#dropdown').val();
$('#dropdown').change(function()
{
   if(previous ==this.value)
  {
      //do things   
  } 
  else
  {
      previous = this.value;
      //do things         
  }       
});

Try this.

Amit
  • 3,251
  • 3
  • 20
  • 31