So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
click() shorthand is deprecated at jQuery 3 The . on() and . trigger() methods can set an event handler or generate an event for any event type, and should be used instead of the shortcut methods.
You have to put the event handler in the $(document).ready() event:
$(document).ready(function() {
$("#btnSubmit").click(function(){
alert("button");
});
});
$(document).ready(function(){
$('your selector').bind("click",function(){
// your statements;
});
// you can use the above or the one shown below
$('your selector').click(function(e){
e.preventDefault();
// your statements;
});
});
$('#btnSubmit').click(function(){
alert("button");
});
or
//Use this code if button is appended in the DOM
$(document).on('click','#btnSubmit',function(){
alert("button");
});
See documentation for more information:
https://api.jquery.com/click/
Try This:
$(document).on('click', '#btnClick', function(){
alert("button is clicked");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnClick">Click me</button>
$("#btnSubmit").click(function(){
alert("button");
});
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#button").click(function(){
alert("Hello");
});
});
</script>
<input type="button" id="button" value="Click me">
<script type="text/javascript">
$(document).ready(function() {
$("#Button1").click(function() {
alert("hello");
});
}
);
</script>
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