0

First of all please ignore if this question if seems stupid as I am not that much good in javascript side.

I need to call a function inside another function on document load. An example code is below. Here I need to alert the message 'my second alert'. How can I make it happen?. I know by initializing alertSecond() inside the function will work it, but is there any other way to do it?

Thanks in advance

$(document).ready() {

test();

function test() {
    alert('my first alert');
    function alertSecond() {
        alert('my second alert')
    }
}

}

Anthony Forloney
  • 90,123
  • 14
  • 117
  • 115
user1817630
  • 135
  • 4

6 Answers6

1

Check This Code..

$(document).ready(function() {
    $('#fun_a').click(myFunction);
});

function myFunction() {
    alert('hi');
}

look out this link..

call user defined function in jquery

Community
  • 1
  • 1
sasi
  • 4,192
  • 4
  • 28
  • 47
0
$(document).ready(function(){
test()
}) ;
function test() {
    alert('my first alert');
    alertSecond();
}
function alertSecond() {
        alert('my second alert')
    }
Pratik Bhatt
  • 871
  • 1
  • 8
  • 21
0

Just call your function from the 1st function and it will work. You don't need to define functions everywhere. When there is no reuse, better not to define code as function.

The following code should work:

$(document).ready(function(){
 test();
});
function test() {
alert('my first alert');
alertSecond();
}
function alertSecond() {
    alert('my second alert');
}
Srihari
  • 766
  • 1
  • 6
  • 22
  • This do not works, it does not display any alert, edit your answer, here is your live code: http://jsfiddle.net/gPNsW/ – sandino Feb 01 '13 at 14:15
  • Check the updated js Fiddle. U cannot run `$` without `jQuery`. Check it now. @sandino – Srihari Feb 01 '13 at 14:24
  • It does not works man!! ..the problem is wrong document.ready invocation you missed function keyword, here is the api: http://api.jquery.com/ready/ – sandino Feb 01 '13 at 14:47
0

Previous answer does not run, it does not display any alert, try this:

function alertSecond() {
     alert('my second alert')
}

function test() {
     alert('my first alert'); 
     alertSecond();
}


$(document).ready(function(){

test();

})

Here is a live example:

http://jsfiddle.net/2xgXG/

sandino
  • 3,813
  • 1
  • 19
  • 24
0

All you need is to call alertSecond() in the test() function.

$(document).ready(function(){
    test();
    function test(){
        alert('my first alert');
        alertSecond();
        function alertSecond(){
            alert('my second alert');
        }
    }
});  

And you can also define test() and alertSecond() out of the $(document).ready function, if you want to call them in other functions.

Xiaodan Mao
  • 1,648
  • 2
  • 17
  • 30
-1

So you declare your two functions like so:

function myFunct1(){
alert('alert 1 from function 1');
}
function myFunct2(){
alert('alert 2 from function 2');
}

Then if you want to call them both one after the other your onload would look like this:

$(document).ready(function(){
myFunct1();
myFunct2();
});

If you wanted to call the second function from inside of the first one your function definition would need to look like this:

function myFunct1(){
alert('alert 1 from function 1');
myFunct2();
}
function myFunct2(){
alert('alert 2 from function 2');
}
user1636130
  • 1,615
  • 5
  • 29
  • 47