Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML make text clickable without making it a hyperlink

I want to add the ability to have the option to click on certain HTML text and have the correct JavaScript code be executed.

How can I do this?

like image 919
Emre801 Avatar asked Feb 27 '12 15:02

Emre801


People also ask

How do I make text clickable in HTML?

To make a hyperlink in an HTML page, use the <a> and </a> tags, which are the tags used to define the links. The <a> tag indicates where the hyperlink starts and the </a> tag indicates where it ends. Whatever text gets added inside these tags, will work as a hyperlink. Add the URL for the link in the <a href=” ”>.

How do you make a non clickable link in HTML?

In order to disable a HTML Anchor Link (HyperLink), the value of its HREF attribute is copied to the REL attribute and the value of HREF attribute is set to an empty JavaScript function. This makes the HTML Anchor Link (HyperLink) disabled i.e. non-clickable.

How do I make text a clickable link?

Create a hyperlink to a location on the webSelect the text or picture that you want to display as a hyperlink. Press Ctrl+K. You can also right-click the text or picture and click Link on the shortcut menu. In the Insert Hyperlink box, type or paste your link in the Address box.


1 Answers

For semantics I'd use a <button> element like this:

<button class="link">Clicky</button> 

to make the button look like normal text you can use CSS:

button.link { background:none; border:none; } 

and for easiness of handing click I'd use jQuery like so:

$(".link").on("click", function(e) {     // e is the event object     // this is the button element     // do stuff with them }); 

Although if you have an ID attribute on the button you can use plain JS like this:

var button = document.getElementById("your-button-id"); button.addEventListener("click", function(e) {     // e is the event object     // e.target is the button element     // do stuff with them }); 
like image 133
JKirchartz Avatar answered Sep 20 '22 19:09

JKirchartz