0

I have a jquery event handler for an element w/ID = submitButton. The code works fine -- but if I click on another button w/ID = yeahTotallyButton then the jquery event handler for the submitButton stops working. No errors show in the console -- but the handler for #submitButton stops firing. The debugger does not stop at breakpoints for submitButton once I have clicked the yeahTotallyButton.

In debugging so far, I have noticed that by commenting out two lines in the event handler for the yeahTotallyButton (indicated in the code below) then the submit button works even after I click the yeahTotallyButton. So basically something in these two lines of code is breaking the submitButton handler. Why is this? How can I fix this? I need to do the things that these two lines of code do in my final website.

<body>

    <div id='header'>
     </div>

      <div id='captchaPanel'>

        <div id='top'>
            <div id='fillerTop'>

            </div>
            <div id='captcha'>
                <img id='captchaText' src='cryptographp.inc.php'> </img>
            </div>
        </div>
        <div id='bottom'>
            <div id='left'>
                <p  id='answerprompt'>Answer: </p>
                <input id="answerBox" type="text" name="firstname">
            </div>
            <div id='right'>
                <table id='buttonTable'>
                <tr>
                    <td><img id='recycleButton' src="images/buttons_recycle.png" ></td>
                </tr>
                <tr>
                    <td><img src="images/buttons_audio.png" ></td>
                </tr>
                <tr>
                    <td><img src="images/buttons_question.png" ></td>
                </tr>
                </table>

                <div id='logo'>
                    <img  src="images/smallLogo.png">
                </div>

            </div>

            <div id='introButtons'> 
                <button id='yeahTotallyButton' type="submit" class="button">Yeah, totally. I am cool person.</button>   
                <button id='imARobotButton' type="submit" class="button">No, I can't come. I'm a robot.</button>
            </div>      

        </div>

    </div>


    <div id='submitDiv'>
    <input id='submitButton' type="submit" class="button" value="Submit"/>
    </div>

    </body>

Here is the script:

           $(document).ready(function () {
                     $("#submitButton").click(function(event) {

                        $.ajax({
                            url: 'getRejection.php',
                           success: function(data) { alert(data) }
                        });
                        $('#captchaPanel').animate({ opacity: 1}, 200);

                        $("#captchaText").attr('src', 'cryptographp.inc.php');

                        alert(event.target.id);

                    });

                    $("#imARobotButton").click(function(){
                     alert("thanks for being honest");
                     location.reload();
                    });

                      $("#yeahTotallyButton").click(function(){
                         $("#introButtons").css('visibility','hidden');

//when these two lines are commented out, 
//then the submit button works even after
// I click the yeahTotallyButton

                 //$("#captchaPanel").css('visibility','visible');
                         // $("#bottom").css('visibility','visible');

                         $("#top").css('visibility','visible');
                         $("#left").css('visibility','visible');
                         $("#right").css('visibility','visible');
                         $("#captchaPanel").fadeIn("fast");              
                         $("#captchaText").attr('src', 'cryptographp.inc.php');
                         $("#top").attr('border-radius', '4px');        
                    });     

                    $("#recycleButton").click(function(){
                         $("#captchaText").attr('src', 'cryptographp.inc.php');
                    });

           });
bernie2436
  • 22,841
  • 49
  • 151
  • 244

2 Answers2

1

My guess is that you somehow end up having more than one element with id set to submitButton, and the button you're checking the click on is not the first in this list. For example, in this scenario...

<div id='submitDiv'>
    <input id='submitButton' type="submit" class="button" value="Alert Submit" />
    <input id='submitButton' type="submit" class="button" value="Alertless Submit" />
</div>

$('#submitButton').click(function() { alert(42); });

... while clicking the first button shows that alert, clicking on the second does nothing.

You can easily patch it by adjusting the selector:

$('[id=submitButton]').click(function() { ... });

Fiddle. But obviously, that'll only mask the real problem: in no circumstances you'd have more than one element with a specific ID in DOM.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
0

The handlers for the yeahTotallyButton were making the captchaPanel element bigger, so that it hung over the submit button -- even though the submitButton was visible. So when you clicked on the submitButton jQuery was not getting the event somehow. Carefully resizing the catpchaPanel solved the problem.

bernie2436
  • 22,841
  • 49
  • 151
  • 244