user275074
user275074

Reputation:

SlideDown then call custom function

Having difficulty understanding why my function is being called twice.

I'm trying to detect when a radio button is called (intRev_yes), check if a div is empty, slide down another div and then call a custome function which dynamically creates a date field.

if(this.id=="intRev_yes"){
 if($('div#mainField').is(':empty')){
  $('.intRevSections').slideDown('slow',function(){
   current=-1;
   addIrField();
  });
 }   
}

When I alert out at each stage, after getting to the end (i.e. addIrField()), the script seems to go back to the slideDown() section and recalls addIrField(). I can't understand why, there are no loops. current is used as an index for the date field.

Upvotes: 1

Views: 1728

Answers (5)

Nat
Nat

Reputation: 1

Jquery animate complete worked for me

 $('.intRevSections:first').slideDown('slow',function(){
    complete: addIrField();
 });

Upvotes: 0

Ali Habibzadeh
Ali Habibzadeh

Reputation: 11568

try this:

if (this.id == "intRev_yes") {
        if ($('div#mainField').is(':empty')) {
            $('.intRevSections').slideDown('slow', function(event) {
                event.stopPropagation();
                current = -1;
                addIrField();
            });
        }
    }

Upvotes: 0

naugtur
naugtur

Reputation: 16915

Sinan already discovered the problem, but did not in fact post an answer. the answer is:

Find out why You have two divs (if You create them from JS the other function might be run twice as well)

You may also want to fix the code so that it uses only one div even if You have two of them

 $('.intRevSections:first').slideDown('slow',function(){
   current=-1;
   addIrField();
  });

Upvotes: 0

Sinan
Sinan

Reputation: 11563

Another case would be:

If you have two elements having class name .intRevSections slideDown would run twice, and callback would fire twice...

Hope this helps, Sinan.

Upvotes: 1

Philippe
Philippe

Reputation: 6828

You are probably calling this from a Dom event that bubbles. See http://en.wikipedia.org/wiki/DOM_events to check if that's the case.

Upvotes: 1

Related Questions