How to make a word act as a button?

Hi, I’d like to know how can I make a link to a method. I mean, not a link that would normally render a new page, but something that looks like a link (a blue word) but triggers one of my class methods like show a photo at the right side of my website or hide it if clicked again.

Thank you

Hi, I’d like to know how can I make a link to a method. I mean, not a link that would normally render a new page, but something that looks like a link (a blue word) but triggers one of my class methods like show a photo at the right side of my website or hide it if clicked again.

Thank you

For this you would normally use JavaScript. I’d recommend reading up at Mozilla Developer Network: https://developer.mozilla.org/en-US/learn.

Basically you’ll register an ‘onclick’ handler on the link and in that function, change the display style of the photo. For instance:

HTML:

Show Picture

JavaScript (index.js):

var link = document.getElementById(“show-link”);

if (link) { link.onclick = function(event) {

var picture = document.getElementById(“my-picture”);

if (picture) {

if (picture.style.display == “none”) {

picture.style.display = “inline”;

} else {

picture.style.display = “none”;

}

}

};

}

Rails provides a helper method for doing just that: http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#method-i-link_to_function. You’ll still need to write the JavaScript to fetch and display the photo though.