-1

I want to enable/disable an input type="number" but it never changes.

It starts disabled and when I press a input type="radio" I want to enable it.

<input type="number" class="form-control filtros_mapa_ruta" id="n_nodes_ruta" value="2" min="1" disabled>

I see a lot of people have this problem and they usually try with $('input').attr("disabled", true); but is failing too.

The jQuery function, using .prop("disabled", true):

$("#radio_ult_pos").on('click', function() {
    if ($('#radio_ult_pos').is(':checked')) {
        $( ".filtros_mapa_ruta" ).checkboxradio( "disable" );
        $('#n_nodes_ruta').prop("disabled", true);
    }
});
$("#radio_ruta").on('click', function() {
    if ($('#radio_ruta').is(':checked')) {
        $( ".filtros_mapa_ruta" ).checkboxradio( "enable" );
        $('#n_nodes_ruta').prop("disabled", false);
    }
});

Snippet (the checkbox here is not working because I'm using a jQuery UI widget, but they're working well in my code):

function checkboxController() {
  $("#radio_ult_pos").on('click', function() {
    if ($('#radio_ult_pos').is(':checked')) {
      $(".filtros_mapa_ruta").checkboxradio("disable");
      $('#n_nodes_ruta').prop("disabled", true);
    }
  });
  $("#radio_ruta").on('click', function() {
    if ($('#radio_ruta').is(':checked')) {
      $(".filtros_mapa_ruta").checkboxradio("enable");
      $('#n_nodes_ruta').prop("disabled", false);
    }
  });
}

$(document).ready(function() {
  checkboxController();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="col-2" id="filter_container">
  <!-- FILTROS-->
  <legend>Filtros mapa: </legend>
  <label for="radio_ult_pos">Última posición</label>
  <input type="radio" name="radio_select" class="filtros_mapa" id="radio_ult_pos" checked>
  <label for="radio_ruta">Ruta</label>
  <input type="radio" name="radio_select" class="filtros_mapa" id="radio_ruta">

  <legend id="legend_filtro_datos">Filtros datos: </legend>
  <label for="checkbox-2">Goat tracker 1</label>
  <input type="checkbox" name="GOAT_TRACKER1" class="filtros_mapa filtros_mapa_ruta filtros_mapa_ruta_checkb optionNodeFilter" id="checkbox-2" disabled>
  <label for="checkbox-3">Goat tracker 2</label>
  <input type="checkbox" name="GOAT_TRACKER2" class="filtros_mapa filtros_mapa_ruta filtros_mapa_ruta_checkb optionNodeFilter" id="checkbox-3" disabled>
  <label for="checkbox-4">Goat tracker 3</label>
  <input type="checkbox" name="GOAT_TRACKER3" class="filtros_mapa filtros_mapa_ruta filtros_mapa_ruta_checkb optionNodeFilter" id="checkbox-4" disabled>

  <input type="number" class="form-control filtros_mapa_ruta" id="n_nodes_ruta" value="2" min="1" disabled>
  <button type="button" class="btn btn-success" id="filtrar_btn_map">Filtrar</button>
</div>

I tried with $('#n_nodes_ruta').removeAttr("disabled") but is not working too...

Why never change the attribute disabled?

Heretic Monkey
  • 10,498
  • 6
  • 45
  • 102
Lleims
  • 873
  • 7
  • 15

3 Answers3

0

In JS, you can set an event listener on the container div and if the radio buttons are clicked, you can set the disabled property on the input to true or false.

const input = document.getElementById("n_nodes_ruta");

function checkboxController() {

  document.querySelector('.col-2').addEventListener('click', () => {
    if(event.target.id === "radio_ult_pos") {
      input.disabled = true;
    }
    else if(event.target.id === "radio_ruta") {
      input.disabled = false;
    }

  })

}

$(document).ready(function() {
  checkboxController();
}); 
Addis
  • 2,364
  • 2
  • 9
  • 19
0

As @Heretic Monkey said the problem was the jQuery function checkboxradio.

Yes, it was defined, but the problem was that the class filtros_mapa_ruta has the input as well, and that made him fail.

The solution was to call the right class, only with the checkbox.

$(".filtros_mapa_ruta_checkb" ).checkboxradio( "disable");

Thank you

Lleims
  • 873
  • 7
  • 15
-1

you can simply use .attr('disabled', 'disabled') to make it disabled and .removeAttr('disabled') to enable it.