SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JQuery click event fires on second click but not on first

    JQuery click event fires on second click but not on first

    Hi my problem is as described above, I have had this happend before some time ago and it still puzzles me not sure why its happening.
    "Persistence is the path to perfection"

  2. #2
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    We're definitely going to need code samples, a live example, or something.
    I'm the web overlord for Graphic Business Systems

  3. #3
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok heres the basics of it:

    When I do this:
    Code:
    	      $("#flowplayer_play").live('click', function() {
    			 $(this).click(flowplayer_play);
    			});
    and call this function in an external script( which is why I did it for tidyness sake)
    Code:
    function flowplayer_play()
    { 
    var src="Images/pause.png"
      	 $("#flowplayer_play").attr("src", src).removeAttr( "id" ).attr("id", "flowplayer_pause"); 
    	      $f("splash").play();
       
    }
    The weird click thing happens.

    But when I just do plain old:

    Code:
        $("#flowplayer_play").live('click', function() {
    			 $f("splash").play();
       });
    It works fine. But I don't want all of my handlers and functions in the same script, I woudl like to put the handlers in the external script too but they only seem to work on the html page and cease to work when put in the script with the functions.

    BTW the live() is only being used because I am adding these buttons dynamially, they aren't always visible

    [/CODE]
    "Persistence is the path to perfection"

  4. #4
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The problem is with this code here (I think):

    Code:
    $("#flowplayer_play").live('click', function() {
        $(this).click(flowplayer_play);
    });
    That doesn't execute your "flowplayer_play" method -- it assigns a new "click" event listener to the element. So if you just change it to this...

    Code:
    $("#flowplayer_play").live('click', flowplayer_play);
    ...then it should work.
    Last edited by sdleihssirhc; Sep 30, 2011 at 10:21. Reason: poor reading comprehension
    I'm the web overlord for Graphic Business Systems

  5. #5
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much sdleihssirhc, we live and learn eh !
    This happened to me ages ago and I never figured it out, always bugged me. Do you have any idea why the event handlers only work in the main html/php script and not in an external file?
    "Persistence is the path to perfection"

  6. #6
    SitePoint Addict sdleihssirhc's Avatar
    Join Date
    Feb 2009
    Posts
    387
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Let's look at the two scripts again:

    Code:
    // your first attempt
    $("#flowplayer_play").live('click', function() {
        $(this).click(flowplayer_play);
    });
    
    // doing it without any external files
    $("#flowplayer_play").live('click', function() {
        $f("splash").play();
    });
    The problem wasn't that your code was in a different file. It's that, in your first script, you were assigning a "click" event listener instead of firing the function.

    When you just stuck $f("splash").play() in the code, you also got rid of that event listener assigning stuff. To make your first function work, you'd have to...

    Code:
    // change this line:
    $(this).click(flowplayer_play);
    
    // into this:
    flowplayer_play();
    I'm the web overlord for Graphic Business Systems

  7. #7
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey there thanks so much for your input on this, its really got me thinking,such an obvious error but never noticed it, all the best to you
    "Persistence is the path to perfection"

  8. #8
    SitePoint Member
    Join Date
    May 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This help to me.

    Quote Originally Posted by silversurfer5150 View Post
    Hey there thanks so much for your input on this, its really got me thinking,such an obvious error but never noticed it, all the best to you
    $(function(){
    $(document).on('click',"a[rel='external']",function(){
    window.location.href=this.href;
    });
    });


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •