I'm new to jQuery. I want to use a button click event to raise an alert box. This is my code, but it doesn't seem to work.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Jquery Basic</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('submit1').click(function() {
alert("JQuery Running!");
)};
});
</script>
</head>
<body>
<a>JQuery Test Page</a><br>
<input id="submit1" type="button" value="Submit"/>
</body>
</html>
The click() is an inbuilt method in jQuery that starts the click event or attach a function to run when a click event occurs. Syntax: $(selector). click(function);
click() in that it has the ability to create delegated event handlers by passing a selector parameter, whereas . click() does not. When . on() is called without a selector parameter, it behaves exactly the same as .
The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.
In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements: 1. $( "div.
You are missing the #
from your id
selector. Try this:
$(document).ready(function(){
$('#submit1').click(function(){
alert("JQuery Running!");
});
});
Answer already posted by @Rory, but you can also try this
$(document).ready(function(){
$('#submit1').on('click',function(){
alert("JQuery Running!");
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With