Normal style
<a href="#" id="myLink" onclick="myFunc();">click me</a>
function myFunc() {
alert('hello!');
}
jQuery style
<a href="#" id="myLink">click me</a>
$(document).ready(function() {
$("#myLink").click(function() {
alert('hello!');
});
});
I've just started using jQuery, but I'm wondering what the difference between these two is. Should I prefer the jQuery method? Are there any advantages when doing it this way?
Imho I don't like that when reading the html with the jQuery method, there's no clue as to what the link does. I've found the jQuery attach method really useful when attaching to multiple elements (eg. $("#myTable tr").click(function(e) {...});
), but when dealing with a singular element I don't know which I should be using.