html5 / jquery - click event not registering for html button tag

11,615

Solution 1

You need to ensure that your jQuery is wrapped in a <script> tag, and ensure that the function is being loaded, perhaps by a $(document).ready() function, as shown below:

<script type='text/javascript'>
$(document).ready(function(){

    //Your Click event
    $('#dSubmit').click(function(){
        console.log('click');
    });

});
</script>

Working Demo

Solution 2

Actually the code stays wrong. A form can be often submitted by hitting enter inside of a textfield. If you want to make it right you should use the submit event.

<script>
$(function(){
    $('#vehicle_form > form').submit(function(){
        console.log('submit');
    });
});
</script>
Share:
11,615

Related videos on Youtube

mheavers
Author by

mheavers

Updated on June 04, 2022

Comments

  • mheavers
    mheavers almost 2 years

    I am trying to validate a form, but I can't get any code to execute when my button is clicked. Here's my code:

    <div id="vehicle_form">
        <form method="post" name="emailForm" action="">
        <input class="dField" id="dfEmail" type="email" name="email" value="Email" onfocus="clearInput(this);" onblur="restoreInput(this)"><br/>
        <input class="dField" id="dfName" type="text" name="firstname" value="First Name" onfocus="clearInput(this);" onblur="restoreInput(this)"><br/>
        <input class="dField" id="dfLast" type="text" name="lastname" value="Last Name" onfocus="clearInput(this);" onblur="restoreInput(this)"><br/>
        <button type="button" id="dSubmit">Submit</button>
        </form>
    </div>
    
    $('#dSubmit').click(function(){
        console.log('click');
    });
    
  • mheavers
    mheavers over 12 years
    Ah - that was the problem. It was inside a script tag but not inside document ready.