Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group JS events

I currently have four change() events that fire exactly the same code (see below), so I'm wondering if there is a way of grouping these events in one line to save reapeating code four times?

Thanks.

/** Check for a change to any of the search dropdowns */
$('#job-roles').change(function(e){
    letter = $_GET('staff_search');
    initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#ind-services').change(function(e){
    letter = $_GET('staff_search');
    initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#bus-services').change(function(e){
    letter = $_GET('staff_search');
    initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#indi-services').change(function(e){
    letter = $_GET('staff_search');
    initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
like image 286
David Gard Avatar asked Dec 07 '22 18:12

David Gard


2 Answers

$('#job-roles, #indi-services, #ind-services, #bus-services').change(function(e){
    letter = $_GET('staff_search');
    initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
like image 89
Craig Avatar answered Dec 10 '22 11:12

Craig


Option 1: give them all the same class and attach the event to all of them $('.someClass').change(...)

Option 2: Group the event: $('#job-roles, #ind-services, ... ').change(...)

like image 36
Naftali Avatar answered Dec 10 '22 13:12

Naftali