I'm rather new to jQuery but i want to know what the best practice is when making a button do something.
Should i use .click()
HTML
<button id="submit" class="btn btn-default">Submit</button>
JS
$('#submit').click(function(){
//DO SOMETHING
});
Or call a function
HTML
<button class="btn btn-default" onclick="Submit();">Submit</button>
JS
function Submit() {
//DO SOMETHING
}
My question is, do these two methods behave the same way? If not, what are the advantages of one over the other.
*I've tried and seems to work the same way but being new to jQuery i wanted to make sure.
addEventListenerbe etc..click()always works better#submitthey would all perform//DO SOMETHING?onclick="Submit();"Submit has to be a global function (attached to window) which is bad practice. All your functions should be wrapped somehow to keep them out of non-namespaced globals., obviously there are ways around this with a namespaced global property. One caveat, if you are using a framework like rivets or angularJs you would use something like ng-click or rv-click inside the element.