4

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?

That Brazilian Guy
  • 3,328
  • 5
  • 31
  • 49
Andrew Smith
  • 310
  • 3
  • 6
  • 19

3 Answers3

4

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.

Community
  • 1
  • 1
That Brazilian Guy
  • 3,328
  • 5
  • 31
  • 49
2

You can add click handler to any element, not only a button.

<span onclick="showMsg()">What is this?</span>
-3

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.

Guest
  • 11
  • HTML 3.2, a link to the top of the page, and intrinsic event attributes. Everything about this is obsolete. It's also wrong since the question is asking about *clicking* and this uses onmouseover. – Quentin Apr 20 '15 at 15:52