0

sorry for bad English.How to get value of an select box if user change a select box or just hover on select box with a single event. i have tried this one but its not working

jQuery("select[name*=super_attribute]").on('change , hover', function(){
 alert('trigger'); 
});

I know we can do like this make separate functions

jQuery("select[name*=super_attribute]").on('change', function(){
 alert('trigger'); 
});

And

jQuery("select[name*=super_attribute]").on('hover', function(){
 alert('trigger'); 
});

But i want to do with a single event not separately because i will do same functionality in both cases.

OBAID
  • 1,299
  • 2
  • 21
  • 43
  • Possible duplicate of [jQuery multiple events to trigger the same function](http://stackoverflow.com/questions/2534089/jquery-multiple-events-to-trigger-the-same-function) – Muhammad Ashikuzzaman Oct 04 '16 at 08:33

3 Answers3

1

There is no need for comma in event listing, comma is used when you have to apply any action for multiple selector

jQuery("select[name*=super_attribute]").on('change mouseover', function(){
  alert('trigger'); 
});

If You have to select multiple element then you can use comma like this

jQuery("select[name*=super_attribute],#xyz").on('change mouseover', function(){
      alert('trigger'); 
    });
Rajesh Patel
  • 1,946
  • 16
  • 20
0

Remove the comma , between the event names - they should be separated with a space only.

jQuery("select[name*=super_attribute]").on('change mouseover', function(){
  alert($(this).val()); 
});

jQuery(document).ready(function() {
  jQuery("select[name*=super_attribute]").on('change mouseover', function() {
    console.clear();
    console.log($(this).val());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="super_attribute">
  <option>1</option>
  <option selected>2</option>
  <option>3</option>
</select>
Satpal
  • 132,252
  • 13
  • 159
  • 168
0

There are two methods to bind elements to multiple events.

  1. You can use .on() to bind a function to multiple events.

    $('#yourelement').on('change mouseover keyup keypress blur', function(e) {
         alert('trigger'); 
    });
    
  2. define a function like

    var myFunction = function() {
      alert('trigger'); 
    }
    

and pass myFunction as a parameter to event function.

$('#yourelement')
.change(myFunction)
.mouseover(myFunction)
.keyup(myFunction)
.keypress(myFunction)
.blur(myFunction)

Note: #yourelement is the id of the element to which event to be binded. and call these methods after document ready.

In your case you can either use $(function(){

    jQuery("select[name*=super_attribute]").on('change mouseover', function(){
     alert('trigger'); 
    });
});

or $(function(){ var myFunction = function() { alert('trigger'); }

$('select[name*=super_attribute]')
    .change(myFunction)
    .mouseover(myFunction);
});
Deepak Sharma
  • 409
  • 4
  • 14