Access form text using javascript

Asked

Viewed 514 times

1

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

<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> 
<script type="text/javascript">
    var randu = 20.00;
    document.getElementById("10").value = randu;
 </script>
  • What do you want to do with document.getElementById("10").value = randu;?

  • change the value of the 10.00 located in : <input name="itemQuantity1" type="hidden" value="10.00">

  • 1

    Did you delete the other question? When so, just edit the current one instead of creating another one.

  • And you want to change when? Why don’t you put that value in HTML directly? Where does it come from 20.00; in var randu = 20.00;? this is compiled on the server?

  • yes comes from randu = 20.00 I edited if you can read again not the code the post.

  • Have you tested document.querySelector('input[name="itemQuantity1"]').value = randu;? I don’t understand where yours comes from 10 here: getElementById("10")

  • No, I’ll try now.

  • I got sergiooooooo, Thank you very much it was worth just waiting 40 minutes after you delete my post kkk, and because I could not put the code, thank you Ucas <3

  • where I place as resolved?

  • and here: <script type="text/javascript"> Document.querySelector('input[name="itemAmount1"]'). value = 200.00; </script> <! -- This is the script-->

  • know why you’re not giving?

  • I gave an answer, take a look. If it solves your problem you can mark as accepted.

  • aaa gave yes agr q li I put 'number' ('20.00')

  • Is there any way I could put it like this?: randu = 20 <script type="text/javascript"> Document.querySelector('input[name="itemAmoun1"]'). value = randu+'. 00'; </script> <! -- This is the script--> ?

  • In that case you do so: document.querySelector('input[name="itemAmoun1"]').value = randu.toFixed(2);

Show 10 more comments

1 answer

4

I don’t understand where the "10" in document.getElementById("10"), because this method seeks elements with id="10".

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

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

so you will select the first element that has an attribute name with the value/name of itemQuantity1. And then you can change yours .value for the amount that randu has.

Notes further that var randu = 20.00; is the same as var randu = 20;. If you want to set the decimals you must use a string: var randu = '20.00';. Another option would be to use the .toFixed(x) that turns numbers into strings, with x decimal place, like this:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.