javascript - Can multiple IDs be included in a jQuery "on click"? -


i have 2 on click event handlers identical:

$('#imgpretravel').on( "click", function() {     $('#pretravel').addclass('finaff-form-help-hide');     $('#posttravel').addclass('finaff-form-help-hide');     $('#posttsec0').removeclass('finaff-form-help-hide'); });  $('#imgposttravel').on( "click", function() {      $('#pretravel').addclass('finaff-form-help-hide');      $('#posttravel').addclass('finaff-form-help-hide');      $('#posttsec0').removeclass('finaff-form-help-hide'); }); 

(the difference being ids - imgpretravel , imgposttravel).

can combine them 1 on click function and, if need comma-separated? iow, should this, similar way multiple classes can assigned element:

$('#imgpretravel #imgpretravel').on( "click", function() {     $('#pretravel').addclass('finaff-form-help-hide');     $('#posttravel').addclass('finaff-form-help-hide');     $('#posttsec0').removeclass('finaff-form-help-hide'); }); 

...or this, if ids arguments in method call:

$('#imgpretravel, #imgpretravel').on( "click", function() {     $('#pretravel').addclass('finaff-form-help-hide');     $('#posttravel').addclass('finaff-form-help-hide');     $('#posttsec0').removeclass('finaff-form-help-hide'); }); 

?

you can this, comma separated selectors.

$('#imgpretravel, #imgpretravel').on('click', function() { [...] 

...would correct approach, though, in example, #imgpretravel same id twice and, no in case. you'll wire first dom element id. see jquery multiple selector (“selector1, selector2, selectorn”) docs more information...

selects combined results of specified selectors.

jsfiddle example - #imgpretravel , #imgposttravel example selectors

<div id="imgpretravel">pre</div> <div id="imgposttravel">post</div> 

$('#imgpretravel, #imgposttravel').on('click', function() {     console.log($(this)); }); 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -