HTML

var ReadOut = document.getElementsByName('ReadOut'); var Currents = 0; var FlagNewNum = false; var PendingOp = ""; // push number function NumPressed(Num) { if (FlagNewNum) { ReadOut.value = Num; FlagNewNum = false; } else { if (ReadOut.value == "0") ReadOut.value = Num; else ReadOut.value += Num; } } 
 <div id="block" style="cursor: move; position: absolute;"> <script src="js/fCalc.js"></script> <form name="calc" action="" id="form" class="colortext" method="POST"> <table class="tblCalc" cellpadding="0 cellspacing=0"> <tr> <td colspan=5 aling="middle"> <input id="editWide" name="ReadOut" type="text" placeholder="enter number" size=40></td> </tr> <tr> <td><input class="number" name="btnSeven" type="Button" value="7" onclick="NumPressed(7)"></td> </tr> </table> </form> </div> 

This is a piece of a form for a calculator. Why does the number 7 not appear when I click the btnSeven button in the ReadOut field? Debugging in the moss, it shows that the NumPressed function is executed, the seven comes, checks are underway and the idea is ReadOut.value + = Num; should display in ReadOut "7". But this is not happening.

  • ReadOut is not an element, but a list of elements, use ReadOut[0] for example - andreymal
  • thanks, it worked - Padawan

1 answer 1

The fact is that you tried to get the value of which is not in ReadOut , i.e. value , ReadOut is a list of elements, you need to get value like this ReadOut[0].value :

 var ReadOut = document.getElementsByName('ReadOut'); var Currents = 0; var FlagNewNum = false; var PendingOp = ""; // push number function NumPressed(Num) { if (FlagNewNum) { ReadOut[0].value = Num; FlagNewNum = false; } else { if (ReadOut.value == "0") ReadOut[0].value = Num; else ReadOut[0].value += Num; } } 
  <div id="block" style="cursor: move; position: absolute;"> <script src="js/fCalc.js"></script> <form name="calc" action="" id="form" class="colortext" method="POST"> <table class="tblCalc" cellpadding="0 cellspacing=0"> <tr> <td colspan=5 aling="middle"> <input id="editWide" name="ReadOut" type="text" placeholder="enter number" size=40></td> </tr> <tr> <td><input class="number" name="btnSeven" type="Button" value="7" onclick="NumPressed(7)"></td> </tr> </table> </form> </div> 

  • why the same code in crome does not work and in mozila works - Padawan
  • @Padawan, this code was tested in Chrome Version 62.0.3202.6 - Puvvl
  • I see. Thanks. Apparently I have something wrong. Even internet exploer worked and crome and Yandex browser do not want)) - Padawan