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?
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.
You can add click handler to any element, not only a button.
<span onclick="showMsg()">What is this?</span>
This is a good JavaScript way of doing it:
`<script language=JavaScript>
<a href = "#" onMouseOver=" alert("THIS IS SPARTA!!");"> What is this? </a>"
</script>`
Make sure to put this within the body of the html.