0

I have two variables :

var Save = document.querySelector('[id$="btnSave"]');
var Cancel = document.querySelector('[id$="btnCancel"]');

How can I attach the same click event to the both ?

$('????').click(function () {
    sessionStorage.clear();
});
4
  • Hi Emma W, if I understand correct you want to execute same function on click of both id’s right? Commented Dec 25, 2017 at 16:31
  • jquery selector = querySelector. so you can do this $('[id$="btnSave"]') see this jsbin.com/pipegawulo/edit?html,js,output Commented Dec 25, 2017 at 16:32
  • @plonknimbuzz not entirely accurate since jQuery has some of it's own pseudo selectors that aren't valid css selectors Commented Dec 25, 2017 at 16:37
  • @charlietfl yes you are right. i just give him simple comparison, since he is new in jquery. btw, ty for your note Commented Dec 25, 2017 at 16:52

1 Answer 1

3

You could use comma separator , for multi selector, like :

$('[id$="btnSave"], [id$="btnCancel"]').click(function () {
    sessionStorage.clear();
});
Sign up to request clarification or add additional context in comments.

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.