change the content of an input with jquery or javascript [duplicate]

0

I want to change the value written in 55 inputs that have the same class called input_valores_provisionales and different id (1,2,3,4 ...) in a function as soon as the page is loaded

here the script

$(document).ready(function()
{
  document.getElementsByClassName("input_valores_provisionales").value="0";

    $(".input_valores_provisionales").attr("value","0");//linea que no funciona

//cada vez que el usuario oprime una tecla en cualquiera de los input
  $('.input_valores_provisionales').keyup(
    function()
    {
      var total = 0;
      //recorremos los input para que haga la suma
      $(".input_valores_provisionales").each(
        function()
        {
          //compruebo que lo digitado sea un numero
          if (!isNaN($(this).val()))
          {
            //convierto a entero la cadena del input en base 10
            var numero_entero = parseInt($(this).val(),10);
            total = total+numero_entero;
            //muestro la suma en el span
            $("#total_provisionales").text(total);
          }
          else
          {
              alert("Debe ingresar un valor numerico");
              $(this).val('0');
          }
        }
      );
    }
  );
});

I tried the following ways and it does not work either

 $('.input_valores_provisionales > input').val("0");
    $(".input_valores_provisionales").text("0");
    document.getElementsByClassName("input_valores_provisionales").value="0";

notwithstanding this form if it works, and once I check that the class is well written, otherwise this would not be effective

$(".input_valores_provisionales").attr("placeholder","0");

but it does not help me because the zero does not take it as a numerical value, it is rather a 'phantom zero' that is displayed there and disappears when putting the cursor over the input, according to this the problem for me is not in that the jquery does not do its respective function if it does not seem that the input was "blocked" to write in the jquery or javascript, with the keyboard I can write normally

I tried to put the attribute value="0" to each of the 55 inputs and it did not work either

I tried to remove it and add the attribute type="text" and neither

Any idea what it can be?

    
asked by Gabriel Uribe Gomez 06.04.2018 в 16:01
source

1 answer

0

If I reply your code, it works (it is not necessary to include the id for this demonstration):

$(document).ready(function()
{
  $('.input_valores_provisionales').val("10"); // sí funciona

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="input_valores_provisionales">
<input type="text" class="input_valores_provisionales">
<input type="text" class="input_valores_provisionales">
<input type="text" class="input_valores_provisionales">
    
answered by 06.04.2018 в 16:03