1
 $("#loggedinNav").click(function (e) {
     // do something
    });

this is my and i want to use same click functionality for other buttons, so can I achieve this behaviour like in this manner:

$("#loggedinNav" || "#mainlogo").click(function (e) {
     // do something
    });

where || = or

3 Answers 3

6
$("#loggedinNav, #mainlogo").click(function (e) { // do something });
Sign up to request clarification or add additional context in comments.

1 Comment

I would do this, but if you go beyond two or three buttons you might want to consider creating a class that's assigned to the buttons, then use the class name instead of the ids.
3

Alternative to metaforce's answer.

You could do this:

var clickHandler = function(e) { 
    //Do stuff 

    };

$("#loggedinNav").click(clickHandler);
$("#mainlogo").click(clickHandler);

This would be better if you have a lot of buttons that need the handler or you want to do it in multiple functions.

1 Comment

yup, it's the same solution, but if your click handler is performing same actions, then it is better to do the same way as suggested earlier ... this'll save time when you want to modify your the action in future ...
1

you can do $("#loggedinNav,#mainlogo").click(...) as per JQuery Docs

1 Comment

not working, you have to put everything with same double qoutes ... as metioned by metaforce (see above solution)

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.