javascript - Changing innerHTML to value from div's Id captured by it's ClassName -


i have buttons 1-9 , want show numbers on div called "screen". wrote such code, not seem work.

piece of html code buttons:

  <div id="screen"></div>    <div><input type="submit" class="numkey" id="key7" value="7" onclick="enterpin();"></div>   <div><input type="submit" class="numkey" id="key8" value="8" onclick="enterpin();"></div>   <div><input type="submit" class="numkey" id="key9" value="9" onclick="enterpin();"></div>        <div style="clear: both;"></div>    <div><input type="submit" class="numkey" id="key4" value="4" onclick="enterpin();"></div>    <div><input type="submit" class="numkey" id="key5" value="5" onclick="enterpin();"></div>        (... , on ...) 

javascript code:

function enterpin() {     (i=0; i<document.getelementsbyclassname("numkey").length; i++)     {         var numkeyid = + " " + document.getelementsbyclassname("numkey")[i].id;         console.log(numkeyid);         return numkeyid;      }      var getelementid = function(numkeyid)     {         this.numkeyid = numkeyid;         document.getelementbyid("screen").innerhtml = document.getelementsbyclassname("numkey")[numkeyid].id;         console.log("asdasdasd");     }     getelementid(); } 

it should work this:

enter image description here

look @ this example

window.onload = function(){     var max_len = 4,         currentvalue = "",         elscreen = document.getelementbyid('screen'),         adddigit = function(digit){             digit = digit instanceof mouseevent ? this.value : digit;             if (elscreen.innerhtml.length < max_len) elscreen.innerhtml += digit;         },         deldigit = function(){             elscreen.innerhtml = elscreen.innerhtml.slice(0,elscreen.innerhtml.length - 1);         };     //setting handlers numkeys     numbtns = document.getelementsbyclassname('numkey');     (var = 0 ; < numbtns.length; i++) numbtns[i].onclick = adddigit;     //setting handler backkey     document.getelementbyid('backkey').onclick = deldigit; } 

do not think event handlers first. write simple functions adddigit , deldigit, , after call them handlers.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -