views:

18

answers:

1

Hello, I have been dealing with an odd problem that .click() events happen twice whenever placed in a jQuery Dialog.

My simple test case is below and a live example is here

  <div id="popup" style="display: none">
    <a href="javascript:void(0);" id="testlink">Test Link</a>
    <script type="text/javascript">
      $('#testlink').click(function(){
        alert("Test Link clicked");
        return 0;
      });
    </script>
  </div>
  <script type="text/javascript">
  $(document).ready(function(){
    $('#popup').css('display','block');
    var h=($(window).height()+0.0)*0.9;
    var w=($(window).width()+0.0)*0.9;
    if(w >= 800){
      w = 800;
    }
    $('#popup').dialog({
      autoOpen: true,
      width: w,
      height: h,
      modal: true,
      open: function(event,ui){
        $('body').css('overflow', 'hidden');
      },
      close: function(event,ui){
        $('body').css('overflow', 'scroll');
      }
    });
  });
  </script>
+1  A: 

Move the <script> block that registers the click event outside of the popup div, I think the JS gets parsed another time when the div becomes visible...

Mike Gleason jr Couturier
Ah, that fixes it as shown here: http://jsbin.com/odago/2
Earlz
Cool, glad I could help! Maybe you can mark the question as answered!? (yeah I'm a whore)
Mike Gleason jr Couturier
Huh. I didn't get the double call running locally with your slim sample, but I did get it on your live sample running on your machine. Your sample site has some extra stuff going on too.
AndrewDotHay
I tested it on Chrome, had it...
Mike Gleason jr Couturier