Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu April 27, 2023 › #HowTo #Javascript

How to display a message when a button is clicked in js

To display a message when a button is clicked in JavaScript, we can use the addEventListener method. The example is given below.

In the above example, we use the addEventListener method to attach a click event handler to the button with the ID "my-btn". When the button is clicked, the function defined as the second argument of the addEventListener method is executed. This function sets the text of the paragraph element with the ID "message" to "This is a message!".

Display a message when a button is clicked using alert() function.#

To display a custom message when a button is clicked using JavaScript, you can also use the alert() function.

In the above example, we add an event listener to the button with the ID "my-btn". When the button is clicked, the function inside the listener is executed, which alerts the custom message "How are you?".

save
listen
AI Answer
Write Your Answer
loading
back
View All