Skip to content
the poor coder

simple alert program in javascript

Poor Man
Published
Reading time
2 min read
On this page

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:

Text
<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:

Text
<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:

Text
<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:

Text
<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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC