tags:

views:

14

answers:

1

I can't show a live example at this point, but i don't see how the slideToggle is being called twice. it is called on click of a link.

here:

// initialize the jquery code
 $(function(){
  $('div.slideToggle a').click(function(){
   $(this).parent().siblings('div.remaining-comments').slideToggle('slow',function(){ //'this' becomes the remaining-comments.
    var hidden = $(this).siblings('div.slideToggle').find('input').val();
    if($(this).siblings('div.slideToggle').find('a').html().substr(0,4) == 'Show'){
     $(this).siblings('div.slideToggle').find('a').html('Hide comments');
    }
    else {
     $(this).siblings('div.slideToggle').find('a').html(hidden);
    }
   });
  });
 });

it is meant to display and hide extra comments on a blog page. however, it is displaying and then hiding in one click. I have put an alert in both the 'if' and 'else' and both appear, so how is it invoked twice?

obviously when the link contains the text 'Show' it reveals the hidden div, and when clicked again it will not find 'Show' and will therefore hide the div. The funny thing is, it was working absolutely perfectly. It's on the company intranet so i suppose maybe something else could be affecting it, but i really don't see how.

A: 

It is possible that your document ready is called more than once, when page reinitializes, best thing to do is to unbind the click event before binding a new one to it.

this way you avoid having the click event bound twice on 1 element

try this:

$('div.slideToggle a').unbind('click').click(function(){
  // your code here
});
Sander
This isn't the best solution...treat the problem not the symptoms, if it's being run twice, *why*? No reason to run all the `ready` handler code twice, it's very inefficient. Also what if other `click` handlers are bound to that element?
Nick Craver
i think it must be to do with .ready being called more than once. our pages are amalgamations of tons of different code snippets. so reusable functions are put in individually calling .ready. the unbind did the trick for me, thanks.
Alex B
I know that Nick, but i have no idea why his ready statement was run twice, so I could not hint into the direction of a possible fix for that. allthough i should have added that this was a workaround rather than a fix per se.
Sander