I have the simple text: "What is this?" on a HTML page.
Without making it a button, is there a way to simply prompt a message box when the user clicks the text?
The simplest way is using Javascript:
<a href="#" onClick="alert('Hello World!');">What is this</a>
(The #
will make the page jump to the top after the click, but there are ways to prevent it, and some will discourage its usage. The JavaScript can be put on the href
as well, but it's not considered good practice.)
But it doesn't necessarily need to be a <a>
anchor, it can be any HTML element:
<span onClick="alert('Hello World!');">What is this</span>
If it is not a <a>
, <button>
or any other element where it is usual to indicate user interaction, you need to stilize it or otherwise indicate it is clickable somehow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With