0

I can only assign events inline in html. The external script is being read as the functions housed in the script file are being executed.

I have tried assigning with the following methods:

  • $('link-1').on('mouseover', boldLink("link-1"))
  • document.getElementById('link-1').onmouseover(boldLink("link-1")
  • document.getElementById('link-1').addEventListener('mouseover', boldLink('link-1'))

I have tried housing the event listeners in a window.onload() function as well. Sometimes it will fire once (even when the mouse has not been over the element), but otherwise it doesn't work.

I have tried moving my <script type="text/javascript" src="/script.js"></script> to the end of the body. Currently it's in the header causing the same problems.

This is happening with all event types as well, not just the mouseover event.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • You are assigning the result of _calling_ the function to the listener rather than a reference to the function. – Andy Jul 07 '21 at 09:32
  • Additonally to correct event handler reference, you've to put the script at the end of the body, or rather when using jQuery, enclose everything inside `$(document).ready(function () {});`. – Teemu Jul 07 '21 at 09:32
  • 1
    Also you could just use CSS. Add a class to whatever elements need this and use `.thisclass:hover { font-weight: bold; }`. – Andy Jul 07 '21 at 09:34
  • To help wrap your mind around this: consider `alert( Math.sqrt(9) );` and think about the order in which things are evaluated / happen. –  Jul 07 '21 at 09:36
  • Thanks everyone, came right with all of this :) – tacitoak82 Jul 07 '21 at 10:07

0 Answers0