I was trying to call the click event when hitting spacebar on keyboard on an anchor like so.
$("a").on("keypress", function (e) {
if (e.which === 32) {
$(this).click();
e.preventDefault();
}
}
This does not work however. I finally figured out a fix but I don't understand why it works. The simple fix was changing $(this).click()
to $(this)[0].click()
What is the [0]
doing and how is it making the click event work on the anchor?
Note: I also tried $(this).trigger("click")
with no luck either.