Add multiple elements to a single var and detect if any of those are clicked?
I have a page that can have one of three possible elements. I would like
to assign whatever element exists to a var and then check if the var is
clicked. I tried using the add(), but it has confused me:
var testingVar =
$('#element-one').find('.object').add('#element-two').find('.object').add('#element-three').find('.object');
$(testingVar ).click(function() {
alert('works');
});
But it seems to me that the add() overwrites the previous add()? if I am
on a page that has #element-three, it works, if on a page with element-one
or element-two, it doesn't. If I change the var to
var testingVar = $('#element-one').find('.object');
Then a page with element-one obviously works.
Can someone help me understand how to use the add() properly in this case?
Thanks
No comments:
Post a Comment