Somar inputs dentro de fieldset javascript

Add comments

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function id( el ){
  5. return document.getElementById( el );
  6. }
  7. function soma(){
  8. var inputs = id(‘campos’).getElementsByTagName(‘input’);
  9.  
  10. var soma =0;
  11. for( var i=0; i<inputs.length; i++ ){
  12. soma += parseInt( inputs[i].value );
  13. }
  14. id(‘resultado’).value = soma;
  15. }
  16. window.onload = function(){
  17. id(’somar’).onclick = function(){
  18. soma();
  19. }
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <form action="" method="post">
  25. <fieldset id="campos">
  26. <label><input type="text" name="valor[]" value="2" /></label>
  27. <label><input type="text" name="valor[]" value="5" /></label>
  28. <label><input type="text" name="valor[]" value="8" /></label>
  29. <label><input type="text" name="valor[]" value="9" /></label>
  30. <label><input type="text" name="valor[]" value="1" /></label>
  31. </fieldset>
  32.  
  33. <input type="button" name="somar" id="somar" value="Somar" />
  34. <label>Total Soma: <input type="text" name="resultado" id="resultado" /></label>
  35. </form>
  36. </body>
  37. </html>
julho 23rd 2010 Não Classificados

Leave a Reply