lukieleetronic lukieleetronic - 6 months ago 8
jQuery Question

jQuery not binding to the form,

I'm having problem with the following code,

The submit button is not binding to the jQuery script and the console is not printing anything. I can't figure out what the problem is..

<html>

<head>
<script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.2.min.js"></script>
</head>

<body>

<script>
// Variable to hold request
var request;

// Bind to the submit event of our form
$("#show").submit(function(event){

console.log ("Im here");

});

</script>
<!--form action="getVehiclePosition.php" method="GET"-->
<!--Name: <input type="text" name="name"><br>-->
<form id="show">
<input type="submit" value="Send">
</form>

</body>
</html>

Answer

You code is not working as at the time the script is interpreted and executed, the form does not exist in the DOM. Wrap your code in document-ready handler.

Specify a function to execute when the DOM is fully loaded.

Use

 $(function () {
    //Your code
 })

OR, You can place the script tag below the form element