Is it possible to have a function within another function like so?
function foo() {
// do something
function bar() {
// do something
}
bar();
}
foo();
function someFunction() { //do stuff } $(document). ready(function(){ //Load City by State $('#billing_state_id'). live('change', someFunction); $('#click_me'). live('click', function() { //do something someFunction(); }); });
You can directly write your jQuery code within a JavaScript function.
To trigger the onclick function in jQuery, click() method is used. For example, on clicking a paragraph on a document, a click event will be triggered by the $(“p”). click() method.
So a jQuery function, which is prefixed with the $ or the word jQuery generally is called from within that method. $(document). ready(function() { // Assign all list items on the page to be the color red.
Yes you can have it like that. bar
won't be visible to anyone outside foo
.
And you can call bar
inside foo
as:
function foo() {
// do something
function bar() {
// do something
}
bar();
}
Yes, you can.
Or you can do this,
function foo(){
(function(){
//do something here
})()
}
Or this,
function foo(){
var bar=function(){
//do something here
}
}
Or you want the function "bar" to be universal,
function foo(){
window.bar=function(){
//something here
}
}
Hop this helps you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With