How to add a custom event to an element
Menu How to add a custom event to an element
 

How to add a custom event to an element

Adding events to elements such as buttons and text within "quote" boxes is a useful documentation technique.

Multiple Clickable text links

On my page about the murals in Belchamp Walter Church I have added multiple event handlers to link to images by Stiffleaf on the Ipernity website.

The code below takes the visitor to the Jerusalem page.

Top

Adding a hyperlink to an element

While this is quite cute, it results in a confusing situation as the visitor does not expect this.

A link should look like a link as that is what most users expect. Putting a hyperlink on a block of text, as in a quote, is confusing.

In any case the link to the external website is now dead and the example above is now a moot point.

The code to do this

The first thing is to select the element by id. In this case "ip1".

and then an Eventlistener is added to it.......

The script used on the wall paimtings page

<script>
document.getElementById("ip1").addEventListener("click", function()
    { location.href = "jerusalem.html" })
</script> !-- Gate of Jerusalem -->

Capturing Function Keys

As an extension to this technique I was wondering how you would capture an F1 press for example.

Links

Top

References: - a note on these

  • The Norfolk Medieval Graffiti Survey - http:// www.medieval-graffiti.co.uk/
  • Beliefs and superstitions medieval graffiti - http:// www.guildchapel.org.uk/wp-content/uploads/2017/02/ Beliefs-and-superstitions-medieval-graffiti-March-2018.pdf
  • javascript-capturing-function-f1-12-keys - https:// https://stackoverflow.com/questions/6912309/ javascript-capturing-function-f1-12-keysstackoverflow.com/ questions/6912309/javascript-capturing-function-f1-12-keys
  • JavaScript Keycode List – Keypress Event Key Codes for Enter, Space, Backspace, and More - https://www.freecodecamp.org/news/ javascript-keycode-list- keypress-event-key-codes/

Site design by Tempusfugit Web Design -