Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery bind on form submit is not working

Tags:

jquery

I have a form like this:

<form class="category_form" id="category_form" method="post">
<p class="ui-widget">
    <label for="birds">Birds: </label> 
    <input id="birds" /> <a id="add_category" href="#">Add Category</a>
</p>

<p class="ui-widget" style="margin-top:2em; font-family:Arial">
    Result:
    <textarea id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></textarea>
</p>

<p>
    <input type="submit" class="button" value="Add">
</p>
</form>

and I have a jQuery bind function like this:

$('#category_form').bind('submit',function()
{
    // Get the variables

    alert ("hello");

    return false;
});

What I wanted to have happen is every time submit button is pressed, the jQuery function would fire and the alert would happen, but for some reason it isn't working as I thought it would.

The live example is on this test page: http://problemio.com/test.php

Any idea why pressing the add button does not make the above jQuery work?

Thanks!!

like image 215
GeekedOut Avatar asked Dec 04 '22 19:12

GeekedOut


1 Answers

your binding should be wrapped in $(document).ready or it attempts to bind the form before the form exists on the page:

$(document).ready(function(){
    $('#category_form').bind('submit',function(){
        ...
    });
});
like image 59
Will Avatar answered Dec 27 '22 10:12

Will