Working with variables

Working with variables

Another question I have is why am i unable to assign an event handler to a variable and work with it. Instead of doing this

  1. var eventTarget = $("#evtTarget");
  2.  $(function() { $("#evtTarget").on("mouseover mouseleave", highlighted); });
  3.  function highlighted () { $("#evtTarget") .toggleClass("highlighted"); }

Id rather assign eventTarget and use that variable as a handler. But for some reason its not working??

  1. var eventTarget = $("#evtTarget")
  2.  $(function() { eventTarget.on("mouseover mouseleave", highlighted); });
  3.  function highlighted () { eventTarget.toggleClass("highlighted"); }