simple alert program in javascript

Simple Alert Program in JavaScript

JavaScript is a powerful programming language that can be used to create dynamic and interactive web pages. One of its most basic functions is the ability to display alerts, which can be used to provide users with important information or prompt them to take action. In this article, we will look at how to create a simple alert program in JavaScript.

Step 1: Create a Button

The first step in creating our alert program is to create a button that will trigger the alert. We can do this using HTML and CSS as follows:

<div id="alert-button">
    <button onclick="showAlert()">Show Alert</button>
</div>

<style>
    #alert-button {
        text-align: center;
        margin-top: 50px;
    }
    
    button {
        padding: 10px 20px;
        border: none;
        background-color: #007bff;
        color: #fff;
        font-size: 16px;
        cursor: pointer;
    }
</style>

This code creates a div container with an ID of "alert-button" and a button inside it. The button has an onclick event that calls the "showAlert()" function when clicked.

Step 2: Create the Alert Function

Now that we have our button, we need to create the JavaScript function that will display the alert. We can do this using the following code:

<script>
    function showAlert() {
        alert("Hello World!");
    }
</script>

This code defines a function called "showAlert()" that displays an alert with the message "Hello World!" when called.

Step 3: Put It All Together

Finally, we need to put our HTML and JavaScript code together to create the complete alert program. Here is what the final code looks like:

<div id="alert-button">
    <button onclick="showAlert()">Show Alert</button>
</div>

<style>
    #alert-button {
        text-align: center;
        margin-top: 50px;
    }
    
    button {
        padding: 10px 20px;
        border: none;
        background-color: #007bff;
        color: #fff;
        font-size: 16px;
        cursor: pointer;
    }
</style>

<script>
    function showAlert() {
        alert("Hello World!");
    }
</script>

When you load this code in your browser, you will see a button labeled "Show Alert". When you click this button, an alert box will pop up with the message "Hello World!".

Alternatively, you could also use the following code to create the same alert program:

<button onclick="alert('Hello World!')">Show Alert</button>

This code creates a button with an onclick event that displays an alert with the message "Hello World!" when clicked. This is a more concise way of achieving the same result.

Conclusion

In summary, creating a simple alert program in JavaScript is easy and can be done using just a few lines of code. By following the steps outlined in this article, you should now have a basic understanding of how to create alerts in JavaScript and how to put together HTML, CSS, and JavaScript code.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe