user372551
user372551

Reputation:

jquery Selectors

I'm writing a simple code using jQuery, in which I need to call a function for a series of selectors and eliminate some functions being called for a particular selector, for example

$('#id1, #id2, #id3').click(function() {
 function1();
 function2();
 function3();
 // rest of my code 
});

I don't wanna call some functions for a particular selector and for a particular reason, say I want to call all the functions when I perform click event on selectors like id1 and id2 and want to eliminate function2() being called when I perform a click event on id3

Thanks for reading

Upvotes: 0

Views: 334

Answers (3)

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

separate them:

$('#id1, #id2').click(function() { 
 function1(); 
 function2(); 
 function3(); 
 // rest of my code  
}); 
$('#id3').click(function() { 
 function1();  
 function3(); 
 // rest of my code  
}); 

OR detect which one and put in a conditional:

$('#id1, #id2, #id3').click(function() { 
 function1(); 
 if ($(this).attr('id')!=id3)
 {
   function2(); 
 };
 function3(); 
 // rest of my code  
}); 

Upvotes: 1

jAndy
jAndy

Reputation: 236022

You can check the event target within your click event handler.

For instance:

$('#id1, #id2, #id3').click(function(event) {
  function1();

  if(event.target.id !== 'id2')
     function2();

  function3();
  // rest of my code 
});

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

if you don't want to execute the function2() for the id2 you could do

$('#id1, #id2, #id3').click(function() {
  function1();
  if (this.id != 'id2')
    function2();
  function3();
  // rest of my code 
});

Upvotes: 1

Related Questions