Access form text using javascript

1

I'm trying to put the value of "10" to the value of a variable, You will be able to open the button with this code but the value 10 (quantity of product) but not being changed, if I simply change the 10 by 20, when I open the button it will be 20 the quantity, however I want to change it with the script because I am creating a system that changes the values depending on the option selected.

<form method="post" target="pagseguro" action="https://pagseguro.uol.com.br/v2/checkout/payment.html">  

    <!-- Campos obrigatórios -->  
    <input name="receiverEmail" type="hidden" value="[email protected]">  
    <input name="currency" type="hidden" value="BRL">  

    <!-- Itens do pagamento (ao menos um item é obrigatório) -->  
    <input name="itemId1" type="hidden" value="001">  
    <input name="itemDescription1" type="hidden" value="EloJob">  
    <input name="itemAmount1" type="hidden" value = "100.00">
    <input name="itemQuantity1" type="hidden" value="10">  
    <input name="itemWeight1" type="hidden" value="0">  

    <!-- Código de referência do pagamento no seu sistema (opcional) -->  
    <input name="reference" type="hidden" value="REF1234">  

    <!-- Dados do comprador (opcionais) -->  
    <input name="senderName" type="hidden" value="José Comprador">  
    <input name="senderAreaCode" type="hidden" value="11">  
    <input name="senderPhone" type="hidden" value="56273440">  
    <input name="senderEmail" type="hidden" value="[email protected]">  

    <!-- submit do form (obrigatório) -->  
    <input alt="Pague com PagSeguro" name="submit"  type="image" src="https://p.simg.uol.com.br/out/pagseguro/i/botoes/pagamentos/120x53-pagar.gif"/></form><scripttype="text/javascript">
    var randu = 20.00;
    document.getElementById("10").value = randu;
 </script>
    
asked by anonymous 25.07.2016 / 00:12

1 answer

4

I do not understand where "10" comes from document.getElementById("10") , since this method looks for elements with id="10" .

But since the inputs have other attributes, you can use for example like this:

document.querySelector('input[name="itemQuantity1"]').value = randu;

in this way you will select the first element that has a name attribute with the value / name of itemQuantity1 . And then you can change your .value to the value that randu has.

Note also that var randu = 20.00; is the same as var randu = 20; . If you want to put the decimals you should use a string: var randu = '20.00'; . Alternatively, you could use .toFixed(x) that turns numbers into strings, with x decimal place, like this:

document.querySelector('input[name="itemQuantity1"]').value = randu.toFixed(2);
    
25.07.2016 / 00:24