You can also play with display: none
if you rearrange your html
:
#checkedText {
display: none;
}
.an_radio input:checked~i:before {
position: relative;
content: "f00c" !important;
}
.an_radio input:checked~#checkedText {
display: inline;
}
.an_radio input:checked~#uncheckedText {
display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet" />
<div class="radio-inline an_radio" style="display: inline-block;">
<label class="top" style="min-width: 7px">
<input type="checkbox">
<span id="uncheckedText">voeg toe</span>
<span id="checkedText">toegevoegd</span>
<i class="fas fa-shopping-cart"></i>
</label>
</div>
Why we write a complex code if we can do it with few lines of CSS.
.an_radio input:checked+strong+i:before {
content: "f00c" !important;
}
.an_radio input:checked+strong:after {
content: "toegevoegd";
}
.an_radio input:checked+strong>span {
display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet" />
<div class="radio-inline an_radio" style="display: inline-block;">
<label class="top" style="min-width: 7px">
<input type="checkbox">
<strong> <span>abcd</span></strong>
<i class="fas fa-shopping-cart"></i>
</label>
</div>
The styling is almost complete, but I am still having one more problem. What the css does right now is, it changes the text and changes the icon but puts the icon on top of the text because the text gets changed using position: absolute
.
How do I keep the icon in position?
.an_radio input:checked+span+i:before {
position: relative;
content: "f00c" !important;
}
.an_radio input:checked+span:before {
content: "toegevoegd";
visibility: visible;
}
.an_radio input:checked+span {
position: absolute;
visibility: hidden;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css" rel="stylesheet" />
<div class="radio-inline an_radio" style="display: inline-block;">
<label class="top" style="min-width: 7px">
<input type="checkbox">
<span>voeg toe</span>
<i class="fas fa-shopping-cart"></i>
</label>
</div>
Done, I thought I had already done that but whatever. All good now 🙂
It adds spacing to the text, but it almost solved the problem.