How do I update an input if the checkbox is clicked with javascript?

1

What I need to do (and that still does not work out for me) is that if the client clicks the OriginAirport checkbox in the address DireccionOrigen input, take the value of the checkbox or, alternatively, define a value for that input that is mandatory.

<label><i class="fa fa-map-marker"></i> Origen</label>

<input type="checkbox" name="OrigenAeropuerto" value="Aeropuerto SCL">
<i class='fa fa-plane'></i> Aeropuerto?

<input type="text" name="DireccionOrigen" id="geocomplete_origen" class="form-control input-sm" required/>
    
asked by Felipe Rubilar 03.04.2017 в 15:17
source

3 answers

1

With this you make that when you press the checkbox or the input you put the value of this in the input

function ponValor(){
  valor = document.getElementById("chk").value;
document.getElementById("geocomplete_origen").value = valor
} 
    <label><i class="fa fa-map-marker"></i> Origen</label>

    <input type="checkbox" name="OrigenAeropuerto" value="Aeropuerto SCL" id="chk" onclick="ponValor()">
    <i class='fa fa-plane'></i> Aeropuerto?

    <input type="text" name="DireccionOrigen" id="geocomplete_origen" class="form-control input-sm" onclick="ponValor()" required/>

Although as you say it is a mandatory value, it is that when you submit the form you validate that the value has data with an if and then call the method that sends the data if it has been validated correctly:

if(document.getElementById("chk").value == "" || document.getElementById("chk").value == null){validado = false};
    
answered by 03.04.2017 / 15:23
source
0

Solution to show the value of the checkbock in the input

<script>
function accion(e){
alert(e.value);
var valor_checkbox=e.value;
document.getElementById("geocomplete_origen").value=valor_checkbox;
}
</script>
<label><i class="fa fa-map-marker"></i> Origen</label>

<input type="checkbox" onclick="accion(this);" name="OrigenAeropuerto" value="Aeropuerto SCL">
<i class='fa fa-plane'></i> Aeropuerto?

<input type="text" name="DireccionOrigen" id="geocomplete_origen" class="form-control input-sm" required/>
    
answered by 03.04.2017 в 15:27
0

If you use jQuery you can do it with the following code:

<script>
$(document).ready(function () {
 $("#OrigenAeropuerto").on("click", function () {
  if ($(this).is(":checked") && !$("DireccionOrigen").val()){
   $("DireccionOrigen").val($(this).val())
  }
 })
})
<script>
<label><i class="fa fa-map-marker"></i> Origen</label>

<input type="checkbox" name="OrigenAeropuerto" value="Aeropuerto SCL">
<i class='fa fa-plane'></i> Aeropuerto?

<input type="text" name="DireccionOrigen" id="geocomplete_origen" class="form-control input-sm" required/>

Now if you want to do it with javascript , then use the following code:

<script>
function OrigenAeropuerto_checked() {
 var i = document.getElementById("geocomplete_origen");
 if ( this.checked && !i.value ) {
  i.value = this.value;
 }
}
</script>
<label><i class="fa fa-map-marker"></i> Origen</label>

<input type="checkbox" name="OrigenAeropuerto" value="Aeropuerto SCL" onClick="OrigenAeropuerto_checked()">
<i class='fa fa-plane'></i> Aeropuerto?

<input type="text" name="DireccionOrigen" id="geocomplete_origen" class="form-control input-sm" required/>

It will be your choice which method to use, however it is important to clarify that jQuery tends to make everything much easier although in this example this advantage is not remarkable.

I hope I have helped, Greetings! ...;))

    
answered by 03.04.2017 в 22:24