Anil Kumar
Anil Kumar

Reputation: 2741

trigger change() even on selecting the same value for select field

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?

Upvotes: 7

Views: 10325

Answers (4)

Vibhesh Kaul
Vibhesh Kaul

Reputation: 2613

Do it on click

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

   if(this.selectedIndex == -1) {
      alert('select one answer');
   }
   else{
      // do whatever you want!
   }        
});

Upvotes: 4

roverv
roverv

Reputation: 89

@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');
});

Upvotes: 0

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

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

Upvotes: 2

dpanshu
dpanshu

Reputation: 463

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');
     }
});

Upvotes: 2

Related Questions