I have a button (or icon) which can have three states.
On hover, it changes state and on click, the new state gets transferred to another state. This state is kept until you click again for which it gets back to normal.
The code is working, but it does not look or feel good. Is there any way I can re-factor this?
HTML
<div class="rendering-expert request-list-supported">
<i class="fa fa-cog">1</i>
<a class="hidden" rel="request-list-add" href="/add/1">
<i class="icon fa fa-plus-circle">2</i>
</a>
<a class="hidden" rel="request-list-remove" href="/remove/1">
<i class="icon fa fa-check-circle">3</i>
</a>
</div>
CSS
.hidden { display: none; }
JQuery
$(function() {
$(".rendering-expert.request-list-supported").hover(function() {
if (!$(this).hasClass("has-request")) {
$(this).find(".fa-cog").hide();
$(this).find("[rel='request-list-add']").removeClass("hidden");
}
}, function() {
if (!$(this).hasClass("has-request")) {
$(this).find(".fa-cog").show();
$(this).find("[rel='request-list-add']").addClass("hidden");
}
}).on("click", "[rel='request-list-add']", function(e) {
e.preventDefault();
var toggler = $(this);
var href = toggler.attr("href");
toggler.parent().find(".fa-cog").hide();
toggler.next("[rel='request-list-remove']").removeClass("hidden");
toggler.toggleClass("hidden");
toggler.parent().toggleClass("has-request");
}).on("click", "[rel='request-list-remove']", function(e) {
e.preventDefault();
var toggler = $(this);
var href = toggler.attr("href");
toggler.parent().find(".fa-cog").show();
toggler.prev("[rel='request-list-remove']").addClass("hidden");
toggler.addClass("hidden");
toggler.parent().removeClass("has-request");
});
});
Please note that some parts are stripped in this example. It will only change state in my production if an AJAX request succeeds.
<div>
with three children)? Would it also be acceptable to just have one element whoseclass
changes for each state? \$\endgroup\$