I have a button, “Add to Cart” which sends an ajax request when clicked. After the request returns successfully, the the button is replaced by “In Your Cart”. “In Your Cart” has a mouseover state: “Remove From Cart”.
The design requirement is that “Remove From Cart” only appear when the user physically moves the mouse over “In Your Cart”. If they simply click “Add to Cart” and don’t move the mouse, the button should say “In Your Cart” after the ajax call completes, even if the mouse is still over the element.
A simple mouseover event listener doesn’t work, because it triggers when the element becomes visible. I’m considering counting mouseovers and mouseouts of a wrapper element, in order to determine if the mouseover event is “real”, or just the result of the element becoming visible, but that’s really ugly. Any other ideas for me?
You could do something like this (edit as appropriate for your AJAX call):
HTML:
Javascript:
jsFiddle
UPDATE
Based on OP’s comment below, I’ve update the HTML and Javascript as follows:
Javascript:
The differences here are:
add-cartbutton is disabled while AJAX is simulated and then hidden.mouseoverandmouseouthave been replaced withmouseenterandmouseleave.spanwrapper so that the user mouse behavior can be tracked better since thespannever hides itself.Updated jsFiddle