Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  195] [ 1]  / answers: 1 / hits: 19537  / 11 Years ago, tue, october 8, 2013, 12:00:00

I have a function I'd like to do whenever either user clicks one of the anchor elements, such as this



$('.element').on('click', function(){
// do stuff here
});


and I want to do the same thing if a select element has changed its value, such as this



$('select').on('change', function(){
// do same stuff here
});


I know I could do



$('.element', 'select').on('click change', function(){
// do stuff here
});


but that would also trigger whenever I click on the select element and I don't want to confuse user and do something then, just when the select element value has changed.


More From » jquery

 Answers
51

You don't have to make your function inline.



var doStuff = function() {
// do stuff here
});

$('.element').on('click', doStuff);
$('select').on('change', doStuff);

[#75138] Monday, October 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maureen

Total Points: 151
Total Questions: 110
Total Answers: 110

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
maureen questions
Mon, Oct 11, 21, 00:00, 3 Years ago
Wed, Jun 30, 21, 00:00, 3 Years ago
Thu, Apr 15, 21, 00:00, 3 Years ago
Tue, Mar 16, 21, 00:00, 3 Years ago
Mon, Dec 7, 20, 00:00, 4 Years ago
;