Jquery Click Event Not Firing On First Click, but does on second click, why?

20,816

Solution 1

This is a longshot, but are you running some sort of tracking script? Like webtrends or coremetrics (or even some of your own script, that's globally looking for all clicks)? I ran into a similar problem a while ago, where the initial-click was being captured by coremetrics. Just a thought.

Solution 2

Does it still happen if you comment out all your code and simply have an alert("hi") inside the click function?

Update

I think Sarfaz has the right idea, but I would use the document ready function like so

$(document).ready(function(){
  $("a.reply").click(function() {
    //code
  });
});
Share:
20,816
Hirvesh
Author by

Hirvesh

Updated on February 12, 2021

Comments

  • Hirvesh
    Hirvesh about 3 years

    I've got a jQuery code, which

    $("a.reply").click(function() {
    //code
    });
    

    When I click the link with .reply class the first time, nothing happens. The second time I click, the code inside the click function works.

    The link is being inserted on the page using PHP from a mysql database. so it's not being inserted dynamically.

    Why is this happening? Any solution?

    The BadASS Code:

    $(function(){
    //TextArea Max Width
    var textmaxwidth = $('#wrapper').css('width');
    //Initialize Focus ids To Different Initially
    var oldcommentid = -1;
    var newcommentid = -2;
    //End Of initialization
    
    $("a.reply").click(function() {
            newcommentid = $(this).attr('id');
            if (newcommentid == oldcommentid)
            {
            oldcommentid=newcommentid;
            $("#comment_body").focus();
            }
            else
            {
            $('#comment_form').fadeOut(0, function(){$(this).remove()});
            var commetformcode = $('<form id="comment_form" action="post_comment.php" method="post"><textarea name="comment_body" id="comment_body" class="added_comment_body" rows="2"></textarea> <input type="hidden" name="parent_id" id="parent_id" value="0"/> <div id="submit_button"> <input type="submit" value="Share"/><input type="button" id="cancelbutton" value="Cancel"/></div></form>');
            commetformcode.hide().insertAfter($(this)).fadeIn(300);
            //
            var id = $(this).attr("id");
            $("#parent_id").attr("value", id);
            oldcommentid=newcommentid;
            //dynamicformcreation function
            dynarun();
            //
            }
    
    
    
            return false;
        });
    
            dynarun();
            
            function dynarun()
            {
            //Form Re-Run Functions
            $('#comment_body').elastic();
            texthover();
            $("#comment_form input, select, button").uniform();
            textareasizer();
            $("#comment_body").focus();
            $("abbr.timestamp").timeago();
            return false;
            }
            
            //TextArea Resizer Function
            function textareasizer(){$("#comment_body").css('max-width', textmaxwidth);return false;}
            
            //Other Miscellaneous Functions
            $('.comment-holder').hover(
            function(event) {
                $(this).addClass('highlight');
            },
            function(event) {
                $('.comment-holder').removeClass('highlight');
            }
            );
    
            function texthover()
            {
            $('.added_comment_body').hover(
                function(event) {
                    $(this).parent().parent().addClass('highlight');
                },
                function(event) {
                    $('.comment-holder').removeClass('highlight');
                }
            );
            return false;
            }
    });
    
  • Hirvesh
    Hirvesh about 13 years
    nope, even with document ready and only a simple click function with an alert inside it does not work.
  • yoda
    yoda about 13 years
    that approach is essentially the same.
  • unclenorton
    unclenorton about 13 years
    If it works the second time, document.ready isn't likely to help here, though it is a good practice to use it.
  • Hirvesh
    Hirvesh about 13 years
    isn't $(function(){ supposed to be a shorthand for document ready? :p
  • Hirvesh
    Hirvesh about 13 years
    hmm...good thinking. I have tons of script working in the background. Gonna disable them and see.
  • Hirvesh
    Hirvesh about 13 years
    you nailed it man. There was a form styling script I was using. Removing the script does the trick. Now everything works.
  • Hirvesh
    Hirvesh about 13 years
    the script in question was jquery uniform. pixelmatrixdesign.com/uniform I wish I could kick that developer in the ass.
  • Hirvesh
    Hirvesh about 13 years
    @yoda a common problem with very little explanation of it on the web O_O You can't believe the amount of I was sitting here researching and pulling my hair out.
  • Hirvesh
    Hirvesh about 13 years
    @True North Creative thanks once more...wasn't such a longshot with your guess huh... :p
  • yoda
    yoda about 13 years
    @the_archer: actually there's lots of information about it, but since most people having trouble with it have no substancial knowlenge of javascript, it's hard to google for a solution.
  • Hirvesh
    Hirvesh about 13 years
    @yoda you're right. It's only my 3 weeks into javascript :)