Good day to all, please help to write on pure js analog:

$('div#but input').click(function() { $('.textbox').val($('.textbox').val() + this.value.toString()); }) 

html:

  <input class="textbox" type="text" size="30" value=""> <div id="but"> <input type="submit" id="button" value="1"> <input type="submit" id="button" value="2"> <input type="submit" id="button" value="3"> <input type="submit" id="button" value="4"> <input type="submit" id="button" value="5"> <input type="submit" id="button" value="6"> <input type="submit" id="button" value="7"> <input type="submit" id="button" value="8"> <input type="submit" id="button" value="9"> <input type="submit" id="button" value="0"> <input type="submit" id="button" value="."> </div> 

The code is focused on the latest versions of chromium, only chromium.

  • @Mnwa, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Nicolas Chabanovsky

2 answers 2

Check here

 var d = document, inp = d.querySelector('div#but input'); inp.addEventListener('click', function(){ var txtBox = d.querySelectorAll('.textbox'); for(var i = 0; i < txtBox.length; i++){ txtBox[i].value += this.value.toString(); } }, false); 

PS If you have multiple input elements in the element with id = but, then so:

 var d = document, inp = d.querySelectorAll('div#but input'); function concatVal(){ var txtBox = d.querySelectorAll('.textbox'); for(var i = 0; i < txtBox.length; i++){ txtBox[i].value += this.value.toString(); } } for(var i = 0; i < inp.length; i++){ inp[i].addEventListener('click', concatVal, false); } 
  • I'm afraid I forgot to bring the html-code, and you misunderstood me. - Mnwa
  • @Mnwa, essentially this [does not change] [1]. In order to rewrite the code - you do not need to know your html. [1]: jsfiddle.net/Deonis/eggq60df/3 - Deonis
  • Thank you, help out! - Mnwa
 document.querySelectorAll('#but input').onclick = function() { document.querySelectorAll('.textbox').value += this.value; } 
  • He does not work. = ( - Mnwa