Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
How to hide or show from field upon user choice of radio input in php laravel?

i have created a form for user registration but the form will show or hide extra field if the user choose one of them

i have this field:

 <div class="form-group row" style="display:none">
    <label for="spec" class="col-md-4 col-form-label text-md-right"> 
      {{ __('spec') }}
    </label>
    <div class="col-md-6">
        <input id="spec" type="text" class="form-control @error('spec') is-invalid @enderror" name="spec" value="{{ old('spec') }}" autocomplete="spec" autofocus>
      @error('spec')
          <span class="invalid-feedback" role="alert">
              <strong>{{ $message }}</strong>
          </span>
       @enderror
     </div>
  </div>

which is hidden by default

and i have these radio inputs:

 <div>
    <input type="radio" id="css" name="fav_language" value="CSS">
    <label for="css">CSS</label><br>
    <input type="radio" id="javascript" name="fav_language" value="JavaScript">
    <label for="javascript">JavaScript</label>
 </div>

so if the user chose first radio will hide the first field (spec) and if the user chose the 2nd radio the (spec) field will not be hidden anymore

how to achieve this using if statement?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I mostly agree with Devsi Odedra comment. It would be easier and it would give you more flexibility to use Javascript but you don't have to. There is a solution using CSS only.

To use CSS only the HTML code must be so the div which needs to be hidden or shown has to be a "younger sibling" of the input. In other words the div must be placed at the same level and after the input (no need to be directly after). This way you will be able to combine general sibling selector and :checked pseudo-class selector

.show-only-if-css-is-checked {
  display: none;
}

#css:checked ~ .show-only-if-css-is-checked {
  display: block;
}
<div>
  <input type="radio" id="css" name="fav_language" value="CSS">
  <label for="css">CSS</label><br>
  <input type="radio" id="javascript" name="fav_language" value="JavaScript">
  <label for="javascript">JavaScript</label>

  <div class="show-only-if-css-is-checked form-group row">
    <label for="spec" class="col-md-4 col-form-label text-md-right"> 
      {{ __('spec') }}
    </label>
    <div class="col-md-6">
      <input id="spec" type="text" class="form-control @error('spec') is-invalid @enderror" name="spec" value="{{ old('spec') }}" autocomplete="spec" autofocus> @error('spec')
      <span class="invalid-feedback" role="alert">
              <strong>{{ $message }}</strong>
          </span> @enderror
    </div>
  </div>
</div>

CSS part can even be shorter

#css:not(:checked) ~ .show-only-if-css-is-checked {
  display: none;
}

EDIT: I was tempted to remove laravel tag from the question, but what you're trying to achive can be done as well with Laravel livewire

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda