how to declare a global function in jquery, how do i call it from a page that was loaded in some div on that page using jquery's load() function.
the function is simple in 1st sub page
+-----------------------------------------------+ | main links | +-----------------------------------------------| | +-------------------------------------------+ | | |1st sub page (myfun function is here) | | | +-------------------------------------------+ | | | +---------------------------------------+ | | | | | | | | | | | | | | | | | | | | | | | mybutton clicked myfun called | | | | | | | | | | | +---------------------------------------+ | | | +-------------------------------------------+ | +-----------------------------------------------+
But when i click, nothing happens... here are both function
myfun
myfun(tab_index, acc_id){
alert(tab_index +" | +" acc_id);
}
mybutton
$("#mybutton").click(function(){
var $bottomLineBtn = $(this);
$bottomLineBtn.parent().myfun('2','43234');
})
Can somebody please help me.. its too hard to find the solution.. i've been searching for 3 hours...
==================================================================================
updated
here is the detailed scenario
id=menu
and others id=subpage1
, #subpage1
.#menu2
and #subpage2
#subpage2
by taking only 1 parameter ....which is id of a listmenu.this script is..
$("#menu2 li").click(function(){ $("#subpage2").load($(this).attr('page_link')); } }).filter('#menu2 li:eq(0)').click();
in the loaded page. at extreme bottom, i use 2 buttons, one button works fine, it calls the above function and change the value li:eq(1) .. that i did like this.
$("#bottom_bar #backbtn").click(function(){ var $bottomLineBtn = $(this); $bottomLineBtn.parent().parent().parent().find('#menu2 li:eq(1)').click(); })
... but the other button doesn't work. i want to call a function that belongs with some divs in #subpage1
. but can't access them. i put only a function, next to the above $("#menu2 li").click(function(){
function.
myfun(tab_index, acc_id){
alert(tab_index +" | +" acc_id);
}
but i don't know how to call this function from the 2nd button loaded into #subpage2
does the above scenario make scene.. please try to understand me.. i'm not very good in description...
Why can't you put your function in an external .js file and then import it into your main page?
function setGlobalFunc()
{
window.myfunc = (function(){alert("o hai");});
}
setGlobalFunc();
myfunc();
Function created from within another function, still globaly accessible.
No jQuery required at all.
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