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

4

7

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;
        }
});
Telegonus answered 19/2, 2011 at 10:50 Comment(7)
Going to need more code here, please post the relevant generated HTML.Newkirk
My first thought would be it comes from "event bubbling". Can you post your HTML (use JSFIDDLE for that)?Embryonic
Looks like problem is elsewhere. Does it work if you replace the code inside the click function with alert(1)?Spew
@the_archer: do you get any error on the first click? Can you check that out with firebug or pressing Cntr+Shift+J in FF?Cudlip
on first click, only this appears in console: Lost Focus: <textarea id="comment_body" rows="2" name="comment_body" style="overflow: hidden; max-width: 520px;"> Received Focus: <a id="2" class="reply" href="#comment_form">Telegonus
However, the code code runs perfectly fine in Google Chrome, but doesn't work in FF and IETelegonus
Do you have any other jquery objects on that page?Educable
P
9

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.

Posticous answered 19/2, 2011 at 11:6 Comment(7)
hmm...good thinking. I have tons of script working in the background. Gonna disable them and see.Telegonus
you nailed it man. There was a form styling script I was using. Removing the script does the trick. Now everything works.Telegonus
the script in question was jquery uniform. pixelmatrixdesign.com/uniform I wish I could kick that developer in the ass.Telegonus
@Educable 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.Telegonus
@True North Creative thanks once more...wasn't such a longshot with your guess huh... :pTelegonus
@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.Educable
@Educable you're right. It's only my 3 weeks into javascript :)Telegonus
A
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
  });
});
Aether answered 19/2, 2011 at 10:52 Comment(4)
nope, even with document ready and only a simple click function with an alert inside it does not work.Telegonus
that approach is essentially the same.Educable
If it works the second time, document.ready isn't likely to help here, though it is a good practice to use it.Principally
isn't $(function(){ supposed to be a shorthand for document ready? :pTelegonus
A
0

I just ran into same problem and I resolved my problem by removing:

<script src="bootstrap.js"></script>

you can use bootstrap.min.js

Antimicrobial answered 10/3, 2018 at 3:37 Comment(0)
H
0

Use Inline CSS for hiding div and use JS/jQuery to show . This way Jquery Click Event will Fire On First Click

     <div class="about-block">
      <div class="title">About us</div>
        <div class="" id="content-text" style="display:none;">
            <p>Show me.</p>
        </div>
    </div>

        <script>     
         var x = document.getElementById("content-text");
             jQuery( '.about-block' ).click(function() {
               if (x.style.display === "none") {
                  x.style.display = "block";             

                } else {
                  x.style.display = "none";
                }
             });
      </script>
Hyaena answered 11/2, 2021 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.