7

I've options like this

<select name="hall" id="hall">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>

I'm using jquery function to submit my form

 $('#hall').change(

But when the same value is selected again, I want it to to be triggered. How can i do this?

Anil Kumar
  • 2,521
  • 7
  • 23
  • 40
  • Related: [Getting value of select (dropdown) before change](http://stackoverflow.com/questions/4076770/getting-value-of-select-dropdown-before-change) – Vidya Sagar Aug 11 '15 at 06:22

4 Answers4

4

Do it on click

$('#hall').on('click',function(){

   if(this.selectedIndex == -1) {
      alert('select one answer');
   }
   else{
      // do whatever you want!
   }        
});
Vibhesh Kaul
  • 2,593
  • 1
  • 21
  • 38
2

Try this:

var prevVal;
$('#hall').on('focus', function (){
     prevVal=$( "#hall:selected" ).text();
});

$('#hall').on('change', function (){
     if(prevVal=$( "#hall:selected" ).text()){
          alert('same val selected');
     }
});
dpanshu
  • 453
  • 3
  • 14
2

DEMO

You can do as below:

$("#hall option").on('click',function(){
    $("#hall").trigger('change');
});
$("#hall").on('change',function(){
   alert('changed'); 
});

Note : As per comment here this works well in FF but not in chrome. You can use it at your own risk

Community
  • 1
  • 1
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200
0

@Vibhesh Kaul solution worked for me on windows, but not for my client who is on a mac.

This is my solution:

var previous_value = '';
$(document).on('click', 'select#hall', function () {
  // click on select
  if(previous_value == '') {
    previous_value = $(this).val();
  }
  // click on option, check if same value is chosen
  else if($(this).val() == previous_value) {
    // trigger a change
    $(this).trigger('change');
  }
});

$(document).on('blur', 'select#hall', function () {
  // reset the previous value of the select
  previous_value = '';
});

$(document).on('change', 'select#hall', function () {
  alert('test example');
});
roverv
  • 69
  • 1
  • 5