Show input according to type Radio

1

I am trying to show a input or a textarea depending on a input radio to save the administrative reports. I just want you to give me some guidance, since javascript is not used, considering that it's with javascript or jquery that you can do this, right?

<form action="" method="POST">
<input type="radio" name="gender" value="texto"> &#91;text&#93;
<input type="radio" name="gender" value="imagen"> &#91;image&#93;<br>
<input type="text" name="imagen" placeholder="Imagen para reporte"><br>
<textarea name="texto" placeholder="Nota"></textarea><br>
<button name="add">Agregar</button>
</form>
    
asked by Santiaggo 03.09.2018 в 20:19
source

3 answers

1

One way is using only the CSS selector :checked , which verifies that the option is selected, like the following example:

/* oculto los elementos */
input.i-text, textarea {
  display: none;
}

/* lo muestros si está seleccionado value=imagen */
input.i-radio[value="imagen"]:checked ~ input.i-text {
  display: block;
}

/* lo muestros si está seleccionado value=texto */
input.i-radio[value="texto"]:checked ~ textarea {
  display: block;
}
<form action="" method="POST">
  <input type="radio" name="gender" class="i-radio" value="texto"> &#91;text&#93;
  <input type="radio" name="gender" class="i-radio" value="imagen"> &#91;image&#93;<br>
  <input type="text" name="imagen" class="i-text" placeholder="Imagen para reporte"><br>
  <textarea name="texto" placeholder="Nota"></textarea><br>
  <button name="add">Agregar</button>
</form>

Information

If you need to learn more about CSS selectors, I'll leave you with the following link that explain in detail each one with examples.

    
answered by 03.09.2018 / 20:32
source
0

You can use this in JQuery

if($("#CHECK").prop("checked")){
    $("#caja").show();
}else{
    $("#caja").hide();
}

Greetings:)

    
answered by 03.09.2018 в 20:22
0

Let's see if I understood what you want to do. What I have understood is that you need that the input with names "image" and "text" have to be shown or hidden depending on which of the radio input pulses.

To begin with you would have to hide the two fields that would be shown depending on which pulses. I have used a display: none that I insert through the style attribute from the same HTML element. It is better to do it from CSS but, you already know ^^

HTML

 <form action="" method="POST">
  <input type="radio" name="gender" value="texto"> &#91;text&#93;
  <input type="radio" name="gender" value="imagen"> &#91;image&#93;<br>
  <input id="imagen" type="text" name="imagen" placeholder="Imagen para reporte" style="display: none"><br>
  <textarea id="nota" name="texto" placeholder="Nota" style="display: none"></textarea><br>
  <button name="add">Agregar</button>
</form>

I have changed your code to make it more comfortable for you, adding some IDs to the elements to be displayed so that it is easier to select them from Jquery, and little else.

Then you should play with the value of the radio input to show or hide the elements depending on this

JQUERY

$("input[name='gender']" ).change(function() {
    if ($(this).val() == 'texto') {
    $('#imagen').show();
    $('#nota').hide();
  }
  else {
    $('#imagen').hide();
    $('#nota').show();
  }
});
    
answered by 03.09.2018 в 20:40