I’ve created a jQuery UI button, and would like to add a ‘X’ secondary icon on mouseenter event, and remove the ‘X’ on mouseleave event. Sounds easy.
$("#hover").button({
icons: {
primary: "ui-icon-tag"
}
}).hover(function (event) {
// hover in
$(this).button("option", "icons", { primary: "ui-icon-tag", secondary: "ui-icon-close" });
}, function (event) {
// hover out
$(this).button("option", "icons", { primary: "ui-icon-tag" });
});
The hover event is triggered multiple times if you move the mouse cursor within the button.
It does work in Firefox, however, failed with IE and Chrome.
It’s hard to explain the quirky effect in words but please see the jsFiddle here:
My goal is to achieve consistent effect across all browsers.
Thanks =)
EDIT***
Now i have some clue as to why the hover is broken in IE and Chrome.
Hovering the UI button a few times and inspect the button element in Firebug,
turns out the html tags are flawed by jQuery UI widget..
<button id="hover" class="ui-button ui-widget ui-state-default ui-corner-all ui-state-hover ui-button-text-icons" role="button">
<span class="ui-button-icon-primary ui-icon ui-icon-tag"/>
<span class="ui-button-text">Hover me!</span>
<span class="ui-button-icon-secondary ui-icon ui-icon-close"/>
</button>
<span class="ui-button-icon-primary ui-icon ui-icon-tag"/>
<span class="ui-button-text">Hover me!</span>
<span class="ui-button-icon-secondary ui-icon ui-icon-close"/>
</button>
Note the extra SPANs and the BUTTON closing tag now?
Is this behavior a jQuery UI’s bug?
You can try the example here:
Okay, after fiddling for awhile, finally a workaround that works.. at least that seems decent to me.
And add one line to CSS:
See here for a working example: http://jsfiddle.net/GCme2/4/