How to set alert message on button click in jQuery ?
In this article, we will learn how to create an alert message in jQuery when a button gets clicked. For this, we will use the jQuery click() method.
jQuery CDN Link: We have linked the jQuery in our file using the CDN link.
<script src=”https://code.jquery.com/jquery-3.6.0.min.js” integrity=”sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=” crossorigin=”anonymous”;>
</script>
Example: Create a new HTML file and add the following code to it. In this method, we have set an alert and linked it to a button so that when that button gets clicked then the alert message will be popping out.
HTML
<!DOCTYPE html> < html lang = "en" > < head > < script src = "https://code.jquery.com/jquery-3.6.0.min.js" integrity = "sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin = "anonymous" > </ script > </ head > < body > < button id = "btn" >Click me!</ button > < script > $(document).ready(function () { $("#btn").click(function () { alert("This is an alert message!"); }); }); </ script > </ body > </ html > |
Output:
Contact Us