Button can not mark radio button nor do right in div

Asked

Viewed 75 times

-2

good morning!

I have the script below that is with two flaws:

  1. When I click on buttons the radio Buttons are not being marked
  2. 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

  • @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?

  • I put in an answer to make it easier to visualize

2 answers

0

Your code, in addition to the detail of the selectors I mentioned before, has several improvement points in CSS and javascript.

I will comment on a few points here:

  • Simplify the selector by ID;
  • Add CSS classes to position the element and make the change between those classes instead of doing it with javascript;
  • If you only want to position the element, you can only use the property left, leave zero to be left and a positive value to unlock more "to the right", no need to use right.

See here an example:

$("#sim").click(function() {
  $("#label")
     .removeClass("nao")
     .addClass("sim");
});
    
$("#nao").click(function() {   
    $("#label")
       .removeClass("sim")
       .addClass("nao");
});
div.btnyesno > input[type="radio"] {
        display: none;
    }
    div.btnyesno {
        position:relative;
        display: flex;
        width: 200px;
        height: 70px;
    }
 #containerbtn {
        position:relative;
        display: flex;;
        align-items: center;
        width: 100px;
        height: 50px;;
        border: 1px solid #000;
        border-radius: 500px;
    }
#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;
    }

   .sim {
     left: 0;
     
   }
   .sim:before {
     content: "Sim";
   }

   .nao {
    left: 50px;
   }
   
   .nao:before {
     content: "Não";
   }
<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"class="nao"></div>
    </div>
    <button for="desbloq" id="nao">Não</button>
    <input type="radio" name="bloq" id="desbloq">
</div>

I took the opportunity and I also content with Yes/No based on class, so you don’t need to change anything in the javascript, only the classes, which solve everything else

0

Guy doesn’t even need JS to do it, let alone jQuery, only with CSS vc solves everything.

The text you exchange using the pseudo-element ::after, the button does not have the attribute for as you used, for this uses a label who accepts the for, and put in it the role="button" to give semantics.

Then use the adjacent selector ~ for the CSS rule to activate the corresponding elements by clicking on the label (clicking on the laber with for you activate the radio with the ID correspondent)

inserir a descrição da imagem aqui

Code of the image above:

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 > label {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
  height: 50px;
  cursor: pointer;
  border: 1px solid #999;
  border-radius: 4px;
  background-color: #ddd;
}

div.btnyesno > label#sim {
  left: -10px;
}

div.btnyesno > label#nao {
  right: -10px;
}

/* correções */
#bloq:checked ~ #sim,
#desbloq:checked ~ #nao{
  background-color: red;
}
#bloq:checked ~ #containerbtn > #label {
  left: 5px !important;
}

#label::after {
  content: "não";
}
#bloq:checked ~ #containerbtn > #label::after {
  content: "sim";
}
<div class="btnyesno">
  <input type="radio" name="bloq" id="bloq">
  <input type="radio" name="bloq" id="desbloq">
  <label for="bloq" id="sim" role="button">Sim</label>
  <div id="containerbtn">
    <div id="label"></div>
  </div>
  <label for="desbloq" id="nao" role="button">Não</label>
</div>

  • Look, CSS is not my strong suit. But I have a question here. The radio button, after submitting the form, will have the value of label selected?

  • Yes @Carlosrocha , I believe so! Input is linked to the corresponding label, not Div with the text in this case.

  • So I’m trying to do the same as yours but I have a problem. By removing the JS, the ball does not change position, It seems that the span is not working.. I will put the new code in the fine of the question

  • @Carlosrocha who span Bro?? In your question you don’t even have the tag <span>. In morality, even disgust to answer your question, you can’t copy a code guy.... if it works here because it doesn’t work? You have no idea what you are doing, now you have changed the Label element for a Span for no apparent reason, and the attribute for does not work in the span... Mano na boa, goes with the answer of colleague Ricardo,I believe you have no conditions to deal with this answer

  • I understand your concern @Ugo. But it’s just that I’m not much of a copycat. I like to understand what I’m doing. But it’s good. Thanks for the attention!

  • not wanting to be boring but being. Explain to me just one thing: In the code: #Bloq:checked ~ #containerbtn > #label { left: 5px ! Mportant; }, you are clicking on the label that blocks and activating the input. Ok. Understood. But do not have in the code a click on the unlock label. However, the core ball the same way. Explain it to me and it will be great for me,

  • The pattern is right (right), you set it right here div.btnyesno > div#containerbtn > div#label { right: 5px; ... } So Right is already valid by default. When u mark the Radio of the lock he puts the Left, when you mark the other Radio the Left comes out and the "standard" Right that is valid

  • So, I edited the bottom of my question keeping the original question. I only deleted the part that was the target of the scolding you gave me! But there is something wrong and I can’t find it at all. Technically by the explanation of the previous comment, it was supposed to be working

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.