Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  94] [ 4]  / answers: 1 / hits: 135404  / 13 Years ago, mon, february 27, 2012, 12:00:00

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?


More From » html

 Answers
48

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
});

[#87182] Saturday, February 25, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronans

Total Points: 460
Total Questions: 109
Total Answers: 108

Location: Slovenia
Member since Sat, Sep 11, 2021
3 Years ago
;