0

I want 'div#Welcome a' and 'div#Familiarize a' to point to this JS code:

    e.preventDefault();
    var itemClicked = $(this);
    var id = itemClicked.attr("id");
    id = id.replace(/\D/g,'');
    slider.goToSlide(id);
    return false;

right now i have this:

$('div#Welcome a').click(function(e){

and this:

$('div#Familiarize a').click(function(e){

for the click to go to the specified code. I was wondering if there was a way that anything that i wanted to be linked with this could could be without having to make a new function every time. Is there an ID for certain tags that i can put on? Thank you in advance.

3
  • 2
    You can also use comma-separated selectors - $('div#Welcome a, div#Familiarize a').click(function(e){...}); Commented Feb 19, 2016 at 19:50
  • Or you can add a class to both anchors and bind to that class Commented Feb 19, 2016 at 19:50
  • FYI, you can eliminate the first 4 lines of code, and just do slider.goToSlide(this.id.replace(/\D/g,'')). The first line isn't needed because return false; handles it. Commented Feb 19, 2016 at 19:55

3 Answers 3

5

Make your code block into a named function and pass that to your event handlers:

function myEventHandler(e) {
    e.preventDefault();
    var itemClicked = $(this);
    var id = itemClicked.attr("id");
    id = id.replace(/\D/g,'');
    slider.goToSlide(id);
    return false;
}

$('div#Familiarize a').click(myEventHandler);
$('div#Welcome a').click(myEventHandler);

Or as @mark.hch and @Huangism have pointed out, you can also use comma-separated selectors:

$('div#Familiarize a, div#Welcome a').click(myEventHandler);
Sign up to request clarification or add additional context in comments.

8 Comments

$('div#Welcome a, div#Familiarize a') save you some characters
Isn't there a way that i can make an if or something to validate certain clicks?
Can you be more specific @Kingrune?
Kind of like what @Barmar is saying where you would add a class to a div except could this be done directly to the a tag? an example being "an a tag is click check to see whether it has _____ class and if it does run the function
You could use if(itemClicked.hasClass('whateverclass')) { // do stuff }
|
1

You can use multiple selectors together by separating them with comma.

$('div#Familiarize a, div#Welcome a').click(function(e) {
    e.preventDefault();
    var itemClicked = $(this);
    var id = itemClicked.attr("id");
    id = id.replace(/\D/g,'');
    slider.goToSlide(id);
    return false;
});

Although to simplify things, I suggest giving all the relevant DIVs a common class, and using that in the selector: $('div.tab a').click(...)

Comments

0

You could also give each div the same class and attach the handler to that.

<div class="thisclass">

$('.thisclass').click(function (e) {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.