Disable the readonly attribute

1

I have a select where I select an object and it pulls the value to a input , but I want to ask if the text is the same Other, that the input CodArea can be editable (remove attribute readonly)

<select class="form-control s" id="id_maquinaria" name="" >
    <option value="tshirt"> Camisas</option>
    <option value="pants"> Pantalon </option>
    <option value="other"> Otros</option>
</select>
<?php  echo form_input(['name'=>'CodArea','id'=>'CodArea','class'=>'form-control','readonly'=>'TRUE']);  ?>

Here is my Javacript

$("#id_maquinaria").change(function() {
    var valor = $(this).val(); // Capturamos el valor del select
    var texto = $(this).find('option:selected').text(); // Capturamos el texto del option seleccionado

    $("#CodArea").val(valor);
    $("#maquina").val(texto);
    if ($(this).find('option:selected').text() === 'Otros') {
        $("#CodArea").readOnly = false;
    }
});

I hope you have explained me well

    
asked by MoteCL 25.07.2018 в 18:40
source

1 answer

2

Two things:

  • You must use .attr("readonly", false) to remove readonly .
  • The text of your option has a space at the beginning.
  • Your code would be like this:

    $("#id_maquinaria").change(function() {
        var valor = $(this).val(); // Capturamos el valor del select
        var texto = $(this).find('option:selected').text(); // Capturamos el texto del option seleccionado
    
        $("#CodArea").val(valor);
        $("#maquina").val(texto);
      
        if ($(this).find('option:selected').text() === ' Otros') {
            $("#CodArea").attr("readonly", false); 
        } else {
            $("#CodArea").attr("readonly", true); 
        }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select class="form-control s" id="id_maquinaria" name="" >
        <option value="tshirt"> Camisas</option>
        <option value="pants"> Pantalon </option>
        <option value="other"> Otros</option>
    </select>
    <input name='CodArea' id='CodArea' class='form-control' readonly='TRUE'>
        
    answered by 25.07.2018 / 18:47
    source