-2
good morning!
I have the script below that is with two flaws:
- When I click on buttons the radio Buttons are not being marked
- When you click the button on the right, the ball moves to the left and changes the text. This was what was expected. But when I click the left button, only the text is changing but the ball is not Ind to the right!
Where am I going wrong?
$("div.btnyesno > button#sim").click(function() {
$("div.btnyesno > div#containerbtn > div#label")
.css("right", "5px")
.html("Não");
//$("div.btnyesno > input[type=radio][id=bloq]").prop("checked", true);
});
$("div.btnyesno > button#nao").click(function() {
$("div.btnyesno > div#containerbtn > div#label")
.css("left", "5px")
.html("Sim");
//$("div.btnyesno > input[type=radio][id=desbloq]").prop("checked", true);
});
div.btnyesno > input[type="radio"] {
display: none;
}
div.btnyesno {
position:relative;
display: flex;
width: 200px;
height: 70px;
}
div.btnyesno > div#containerbtn {
position:relative;
display: flex;;
align-items: center;
width: 100px;
height: 50px;;
border: 1px solid #000;
border-radius: 500px;
}
div.btnyesno > div#containerbtn > div#label {
position:absolute;
display: flex;
right: 5px;
width: 45px;
height:45px;
align-items: center;
justify-content: center;
border: 1px solid #000;
border-radius: 500px;
transition: all 500ms;
}
div.btnyesno > button{
position:relative;
display: flex;
align-items: center;
justify-content: center;
width: 50px;
height:50px;
}
div.btnyesno > button#sim {
left: -10px;
}
div.btnyesno > button#nao {
right: -10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btnyesno">
<input type="radio" name="bloq" id="bloq">
<button for="bloq" id="sim">Sim</button>
<div id="containerbtn">
<div id="label">Não</div>
</div>
<button for="desbloq" id="nao">Não</button>
<input type="radio" name="bloq" id="desbloq">
</div>
After Hugo’s explanation I’m trying to do without JS but I found another problem.
In the approach below,
div.btnyesno > input[type="radio"] {
display: none;
}
div.btnyesno {
position: relative;
display: flex;
width: 200px;
height: 70px;
}
div.btnyesno > div#containerball {
position: relative;
display: flex;
align-items: center;
width: 100px;
height: 50px;
border: 1px solid #000;
border-radius: 500px;
}
div.btnyesno > div#containerball > div#ball {
position: absolute;
display: flex;
left: 50px;
width: 45px;
height: 45px;
background: #ccc;
align-items: center;
justify-content: center;
border-radius: 500px;
transition: all 500ms;
}
div.btnyesno > label{
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 50px;
height: 50px;
cursor: pointer;
}
div.btnyesno > label#sim {
left: -10px;
}
div.btnyesno > label#nao {
right: -10px;
}
#bloq:checked ~ #label {
left: 5px !important;
}
#desboq:checked ~ #label {
right: 5px !important;
}
.btnYes {
left: 5px !important;
}
.btnNo {
right: 5px !important;
}
<div class="btnyesno">
<input type="radio" name="bloq" id="bloq" value="bloq">
<input type="radio" name="bloq" id="desbloq" value="desbloq" checked>
<label role="button" for="bloq" id="sim">Sim</label>
<div id="containerball">
<div id="ball"></div>
</div>
<label role="button" for="desbloq" id="nao">Não</label>
</div>
the ball no longer changes position.
But input gets the label click.
$("div.btnyesno > button#sim")
pq make this selector so complex? id must be unique, then just do$("#sim")
, the same goes for others– Ricardo Pontual
@Ricardopunctual, thanks for the tip, makes perfect sense. I think I’ll follow. But, what about the problem? You know how to help me solve?
– Carlos Rocha
I put in an answer to make it easier to visualize
– Ricardo Pontual