I have seen some jquery code where it appears that a click event is triggered programmatically like so:
$sortElements.on('keydown', function(e) {
if (e.which === 13 || e.which === 32) {
e.preventDefault();
if ($(this).is('a')) {
e.target.click();
} else {
$(this).click();
}
}
});
Is this a best practice and or is there a better way to do this?
Also, I have read that the jquery short hand for click is deprecated and the correct way is to write:
$(elem).on("click", function(){})
I don't think it's possible to programmatically trigger a click using this new format however.
I have read that the jquery short hand for click is deprecatedthis is not true at all.onand thetriggerof a click, which may be confusing. You're trying to use the event listener to trigger the event. But as mentioned above, they're both still valid, so you really don't need to change anything.$(this).is('a')=>this.tagName === 'A':|