Is it possible to have multiple click events on one elements, when using different selectors?
<button id="test" class="testclass">test</button>
<button id="test2" class="testclass2">test 2</button>
//only B
$('.testclass')[0].click(function(){alert('A');});
$('#test').click(function(){alert('B');});
// A and B
$('#test2').click(function(){alert('A2');});
$('#test2').click(function(){alert('B2');});
Yes, that’s perfectly possible.
However, you’re misusing jQuery.
Writing
$(...)[0]gives you the first raw DOM element in the set.It isn’t a jQuery object, so you can’t call jQuery’s
clickmethod on it.Instead, you should call the
eqmethod, which returns a jQuery object containing a single element from the original set.Change it to
$('.testclass').eq(0).click(...)