Force require on display:None

Asked

Viewed 326 times

2

I have the following html:

<ul class="payment-methods">
  <li class="payment-method paypal">
    <input name="payment_methods" type="radio" id="paypal">
    <label for="paypal"></label>
  </li>
  <li class="payment-method pagseguro">
    <input name="payment_methods" type="radio" id="pagseguro">
    <label for="pagseguro"></label>
  </li>
  <li class="payment-method boleto">
    <input name="payment_methods" type="radio" id="boleto">
    <label for="boleto"></label>
  </li>
</ul>

That shows payment methods.

In it, the radio buttons are with display:none so that the labels are shown in their place.

There is a way for me to force at least 1 button, to be marked even if they are with display:none?

The result is this: inserir a descrição da imagem aqui

CSS

@charset "utf-8";
/* CSS Document */

.divCheckList .valorBool,
.divCheckList .divCheckBox {
    height: 34px;
    display: inline-block;
    border: rgb(222,222,222) 1px solid;
    vertical-align: middle;
}
.divCheckList .valorBool {
    line-height: 34px;
}
.divCheckList .labelCheckBox {
    position: relative;
    display: block;
    width: 60px;
    height: 34px;
    background-color: rgb(222,222,222);
    border: none;
    border-radius: 34px;
    -webkit-transition: .4s;
    transition: .4s;
    cursor: pointer;
}
.divCheckList .labelCheckBox:before {
    position: absolute;
    display: block;
    width: 26px;
    height: 26px;
    background-color: #FFF;
    border-radius: 50%;
    content: "";
    margin: 4px;
    -webkit-transition: .4s;
    transition: .4s;
}
.divCheckList .divCheckBox .checkBox:checked + .labelCheckBox {
    background-color: blue;
}
.divCheckList .divCheckBox .checkBox:checked + .labelCheckBox:before {
    -webkit-transform: translateX(26px);
    -ms-transform: translateX(26px);
    transform: translateX(26px);
}
  • And if you put the property checked what happens?

  • There is no way to put checked in dislay:None. Can’t find the object!

2 answers

2

I believe the goal is to interact radio along with the image that is displayed, making the image have the behavior of radio. If it is, you can, instead of defining display: none, removing the DOM element, use visibility: hidden, that only the hidden.

What is the difference between display:None and visibility:Hidden?

The point is that the space occupied on the screen by input is not compensated by hiding it and therefore it is also necessary to define its position as absolute; in this way, you will remove the element from the flow natural rendering and your screen space will be compensated.

Take an example:

label {
  cursor: pointer;
}

label > input {
  visibility: hidden;
  position: absolute;
}

label > input:checked + img {
  border: solid 1px red;
}
<label>
  <input type="radio" name="avatar" value="1" checked>
  <img src="http://via.placeholder.com/100x100" alt="">
</label>
<label>
  <input type="radio" name="avatar" value="2">
  <img src="http://via.placeholder.com/100x100" alt="">
</label>
<label>
  <input type="radio" name="avatar" value="3">
  <img src="http://via.placeholder.com/100x100" alt="">
</label>

  • I understood and liked the idea. But require is not working. It is passing directly even not marked!

  • @Carlosrocha you must have done wrong, because I tested it here and it works normally. Play your problem on https://jsfiddle.net and send the link.

  • I think this is it: The full code is in the question: https://jsfiddle.net/tzLvfokq/

  • But you didn’t define the field as required.

  • See in working with required and passing the same way: http://www.hotplateprensas.com.br/styles/

  • Without selecting the payment method, the form is not submitted. That’s not what you need?

  • this. However, require is not working in the payment options. Note that even with the changes require does not work. Goes straight to the next field of the form. That’s why I put the files on the web and the link. To better see the error.

  • What would be "pass to the next field"? Do you say about the error message? If yes, in fact it does not appear, because the field is hidden, but the required works because the form is not submitted without selecting the form of payment. If you want an error message to appear, you should do this manually.

  • Note that below the payment options appear the first type text. And, by clicking the second Submit button, yes, click the second because only it is type Submit, the required type text is triggered I removed the other buttons

  • Debugging on Chrome found this: An invalid form control with name='payment_methods' is not focusable.

Show 6 more comments

0

Set width and height to "0", so it won’t be "display:None" and won’t appear the same way.

  width:0;
  height:0;
  • I had another idea and I would like your opinion: What if, instead of requiring input, you put something equivalent on the label? No way right?

  • you can use jquery by placing event click on the label, you create an Hidden input, then by clicking on the label the event click sends the value to the Hidden input and so you can even leave a default value in the field.

  • Didn’t work because it shows the full size input.

Browser other questions tagged

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