Print the value of button into text -


Well ... I want to include the value of the button when I click on the button, but JS 'undefined' me 'For the button it looks like I want to make a calculator, yes, but the first step in creating the calculator is to insert the value of the buttons into text.

  & lt; Html & gt; & Lt; Top & gt; & Lt; Meta charset = "UTF-8" /> & Lt; Title & gt; My calculator & lt; / Title & gt; & Lt; Style type = "text / css" & gt; & Lt; / Style & gt; & Lt; Script type = "text / javascript" & gt; Function action () {/ * This is to test the function to run the warning ('successful!'); * / Var val = document.getElementsByName ('num'). Value; Document.getElementById (/ * 'result' or 'answer' * /). InnerHTML = val; } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Input id = "results" type = "text" read /> & Lt; Br / & gt; & Lt; Button name = "number" value = "1" onclick = "action ()" & gt; 1 & lt; / Button & gt; & Lt; Button name = "number" value = "2" onclic = "action ()" & gt; 2 & lt; / Button & gt; & Lt; Button name = "number" value = "3" onclic = "action ()" & gt; 3 & lt; / Button & gt; & Lt; Button name = "Operand" & gt; + & Lt; / Button & gt; & Lt; Br / & gt; & Lt; Button name = "number" value = "4" onclick = "verb ()" & gt; 4 & lt; / Button & gt; & Lt; Button name = "number" value = "5" onclic = "action ()" & gt; 5 & ​​lt; / Button & gt; & Lt; Button name = "number" value = "6" onclic = "action ()" & gt; 6 & lt; / Button & gt; & Lt; Button name = "Operand" & gt; - & lt; / Button & gt; & Lt; Br / & gt; & Lt; Button name = "number" value = "7" onclic = "action ()" & gt; 7 & lt; / Button & gt; & Lt; Button name = "number" value = "8" onclick = "action ()" & gt; 8 & lt; / Button & gt; & Lt; Button name = "number" value = "9" onclic = "action ()" & gt; 9 & lt; / Button & gt; & Lt; Button name = "Operand" & gt; * & Lt; / Button & gt; & Lt; Br / & gt; & Lt; Button name = "Operand" & gt; C & lt; / Button & gt; & Lt; Button name = "number" value = "0" onclick = "verb ()" & gt; 0 & lt; / Button & gt; & Lt; Button name = "Operand" & gt; = & Lt; / Button & gt; & Lt; Button name = "Operand" & gt; / & Lt; / Button & gt; & Lt; H1 id = "answer" & gt; & Lt; / H1> & Lt; / Body & gt;  

Can you try this way,

html

  & lt; Button name = "number" id = "9" value = "9" onclick = "action (9)" & gt; 9 & lt; / Button & gt;  

script

  function action (id) {/ * This is to test the function to run the warning ('id!'); * / Var val = document. GetElementById (id). value; Document.getElementById ('Result'). Value + = val; }  

You are also using the same name for many elements


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -