Looking at When should I use jQuery's document.ready function? and Form submit button onclick does not call my JS function gives a sense that using $document.ready(function(){})
should have no harm in the code, rather it is for the good.
But when I enter a function into the onclick
method of button, the functions inside ready()
statement don't get executed. Why so?
As an example, this code does not give an alert
$(document).ready(function(){
function f(){
alert("alert");
}
})
/*function f(){
alert("alert");
} */
<button onclick="f()">
My Button
</button>
I understand that if I use the jQuery selectors to make it work, it will ($("button").click(f)
). But why does this method fail?