ADVERTISEMENT
ADVERTISEMENT

Display different Alert Message on different Button Click Event using JavaScript

In this code snippet we will learn how to display different alert messages using JavaScript by clicking on different buttons. Here we will pass the Button Ids in JavaScript function on Button onClick event and JavaScript function will display alert messages according to Button Ids.

Display Different Alert Messages on Different Button Click Events

HTML and JavaScript:

<!--Display different Alert Message on different Button Click Event.-->
<html>
    <head>
        <title>Display different Alert Message on different Button Click Event.</title>
        <script type="text/javascript">
            function showMessage(btnId) {
                if (btnId == "btnShowMsg1") alert("You clicked on Button 1.");
                else if (btnId == "btnShowMsg2") alert("You clicked on Button 2.");
                else if (btnId == "btnShowMsg3") alert("You clicked on Button 3.");
                else alert("Invalid Button Id.");
            }
        </script>
    </head>
    <body>
        <center>
            <h1>Display different Alert Message on different Button Click Event.</h1>
            <b>Click on button to display message: </b><br />
            <br />
            <input type="button" id="btnShowMsg1" value="Button 1" onClick="showMessage(this.id)" />
            <input type="button" id="btnShowMsg2" value="Button 2" onClick="showMessage(this.id)" />
            <input type="button" id="btnShowMsg3" value="Button 3" onClick="showMessage(this.id)" />
        </center>
    </body>
</html>

Result:

display alert message on button click event

JavaScript Examples »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.