0

I've looked at the CoffeeScript documentation, but can't get this to work.

if $("#main").hover
    alert('clicked')

When I reload the page, the alert always pops up. I have it properly indented in my .js.coffee file, so I'm not sure what I'm missing.

3
  • What are you even trying to do with the if statement? That's not how you'd do something when you hover on an element. Commented Dec 5, 2013 at 20:45
  • I was trying to detect whether or not the element was hovered. Commented Dec 5, 2013 at 20:55
  • Then you should attach an event handler like normal. Commented Dec 5, 2013 at 20:59

2 Answers 2

1

if $(#"main").hover will always return true, that is why the alert is always triggered.

.hover is just returning the "hover" function from jQuery which will evaluate to true in an if statement

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to do something when hover events fire over #main (which could fire a LOT, btw), you might try:

$('#main').hover () -> 
  do_something()

If you just want to determine in a given moment whether or not the mouse is currently hovering #main, you should do:

if $('#main').is(':hover') 
  do_something()

See also Detect IF hovering over element with jQuery

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.