theraneman
theraneman

Reputation: 1630

Calling a function more than once in jQuery

I have a function called "go()" which needs to be called thrice on page load. Right now I have called it on the ready event of a div with class "nav". Can someone suggest how can I call the go function thrice. Adding go() calls one after other calls it only once.

$('.nav').ready(function() {
     go();
    });

Upvotes: 3

Views: 1602

Answers (5)

TequilaSunset06
TequilaSunset06

Reputation: 1

I think I understand what you're trying to do...in the best terms as I can possibly describe since I've not taken a single jQuery class, just learned bits and pieces on my own through places like this website. I was doing something similar. I needed a datepicker to call twice, for two different fields that needed a calendar to pop up - for a check-in and check-out date in a form. In my head I did this, I made two separate scripts with two separate and different id values:

<script>
   $( function() {
     $( "#datepicker" ).datepicker();
   } );
</script>  


<script>
 $( function() {
   $( "#datepicker2" ).datepicker();
 });
</script> 

And my form in the body of my webpage looks like this:

For my Check-In Date:

<input name="DateIn" type="text" id="datepicker" style="width:90px"/>

For my Check-Out Date:

<input name="DateOut" type="text" id="datepicker2" style="width:90px">

So I guess you need to make 3 scripts each with #go1, #go2, #go3 in your head and corresponding calls in your html. I hope this helps.

Upvotes: 0

Rik Heywood
Rik Heywood

Reputation: 13972

Even though you say this does not work, it should...

$('.nav').ready(function() {
     go();
     go();
     go();
    });

Update: If there is an error in your go() function, it might terminate execution before go() returns for the first time. Run in Firebug and see if you are getting any errors.

Upvotes: 4

Justin Ethier
Justin Ethier

Reputation: 134275

As other people have stated, placing the calls one-after-the-other should have called it three times. Perhaps there something that go() is doing that is not being done more than once due to how the function is called, etc. It might be helpful to take a closer look at go() - please post that code and/or explain why you need to call it 3 times.

Upvotes: 2

Rene Saarsoo
Rene Saarsoo

Reputation: 13937

Like rikh sayd, you must be mistaken.

Please test that you can see three hello-s when you replace your go() function with something like this:

function go() {
  alert("hello");
}

Upvotes: 0

Martynnw
Martynnw

Reputation: 10915

$('.nav').ready(function() {
     go();
     go();
     go();
    });

Should call it three times.

Upvotes: 2

Related Questions