Open In App

How to set alert message on button click in jQuery ?

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Setting an alert message on a button click in jQuery means using jQuery to capture the button’s click event and trigger a browser alert box. This alert displays a custom message to the user whenever the button is clicked.

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: In this example we use jQuery to trigger an alert message when a button is clicked. The click() event on the button displays the alert message: “This is an alert message!” when clicked.

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:

Alert msg using click method



Next Article

Similar Reads