html - JavaScript How to Get Keyboard to Push() new Input via Buttons -


the title kinda confusing because find hard explain. basically, can 1 input button have 1 id name javascript function store variable , display replacing empty paragraph tag variable. however, can't work multiple buttons same function, since i'm trying number keypad going buttons, , having display result. think need use push() method somehow add values, click submit button, have resulting string displayed. know how use push known variables, don't know how implement unknown variables, though:

    var fruits = ["banana", "orange", "apple", "mango"];     fruits.push("kiwi"); 

anyways, code here, input buttons 1 button i, , etc. jquery answers welcome, don't prefer jquery, i'm asking javascript.

<!doctype html> <html> <head>     <title>desperate</title>  <script type="text/javascript">          function othername() {     var inputinput = document.getelementbyid("userinput").value;     var n = inputinput.length;     document.getelementbyid("demo").innerhtml = n;    }  </script> </head> <body> <p id="demo"></p> <input id="text" type="text"> </div>        <div>           <input id="userinput" type="button" value="1" onclick="othername();" />           <input id="btn2" type="button" value="2" onclick="othername();" />           <input id="btn3" type="button" value="3" onclick="othername();" />       </div>  </body> </html> 

all appreciated! thank you!

you should bind onclick event handler in different way. can examine incoming event in "othername" function , value of element clicked.

<!doctype html>  <html>  <head>      <title>desperate</title>        <script type="text/javascript">            function othername(e) {              var n = e.srcelement.value || '';              document.getelementbyid("demo").innerhtml = n;          }        </script>  </head>  <body>      <div>          <p id="demo"></p>          <input id="text" type="text">      </div>        <div id="btns">          <input id="userinput" type="button" value="1" />          <input id="btn2" type="button" value="2" />          <input id="btn3" type="button" value="3" />      </div>        <script>          var btnc = document.getelementbyid("btns");          btnc.onclick = othername;      </script>    </body>  </html>


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -