Shows " Uncaught ReferenceError : form is not defined " in the console - javascript

I am a complete beginer, and I am learning frontend web development currently. And as a first project I was creating a simple calculator. I followed steps from a youtuber as I was learning from him. But the problem occuring is that whenever I am clicking buttons it is not displaying the number in the display bar and showing me error "Uncaught ReferenceError : form is not defined". I currently don't have any knowledeg regarding this pls help me out. I am just curious that what is my mistake.
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<form class="form">
<div class="display">
<input name="displayResult" type="text" placeholder="0" />
</div>
<div class="buttons">
<div class="row">
<input class="btn" type="button" name="b7" value="7" onclick="func(b7.value)">
<input class="btn" type="button" name="b8" value="8" onclick="func(b8.value)">
<input class="btn" type="button" name="b9" value="9" onclick="func(b9.value)">
<input class="btn" type="button" name="plus" value="+" onclick="func(plus.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b4" value="4" onclick="func(b4.value)">
<input class="btn" type="button" name="b5" value="5" onclick="func(b5.value)">
<input class="btn" type="button" name="b6" value="6" onclick="func(b6.value)">
<input class="btn" type="button" name="minus" value="-" onclick="func(minus.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b1" value="1" onclick="func(b1.value)">
<input class="btn" type="button" name="b2" value="2" onclick="func(b2.value)">
<input class="btn" type="button" name="b3" value="3" onclick="func(b3.value)">
<input class="btn" type="button" name="mul" value="*" onclick="func(mul.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b0" value="0" onclick="func(b0.value)">
<input class="btn" type="button" name="bd" value="." onclick="func(bd.value)">
<input class="btn" type="button" name="divv" value="/" onclick="func(divv.value)">
<input class="equal" type="button" value="=" onclick="displayResult.value=eval(displayResult.value)">
</div>
</div>
</form>
</div>
<script>
function func(result) {
form.displayResult.value = form.displayResult.value + result;
}
</script>
</body>
in the console that error is being showed for this specific line,
form.displayResult.value = form.displayResult.value + result;

The problem is you have not defined your form variable. In javascript all undeclared variables have a value of undefined.
To fix this, inside your func, just add var form = document.getElementByClassName('form')[0].
Reference : https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

form means nothing to javascript. Therefore, it is not defined. You need to fetch the form in the DOM first:
const form = document.querySelector("form.form");
// would be safer if you gave the form an id but doesn't matter in this case
// then you can change the value
form.displayValue.value += result;
a quick look at document.querySelector

Add let form = document.getElementById('form') in func():
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<form class="form" id="form" )>
<div class="display">
<input name="displayResult" type="text" placeholder="0" />
</div>
<div class="buttons">
<div class="row">
<input class="btn" type="button" name="b7" value="7" onclick="func(b7.value)">
<input class="btn" type="button" name="b8" value="8" onclick="func(b8.value)">
<input class="btn" type="button" name="b9" value="9" onclick="func(b9.value)">
<input class="btn" type="button" name="plus" value="+" onclick="func(plus.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b4" value="4" onclick="func(b4.value)">
<input class="btn" type="button" name="b5" value="5" onclick="func(b5.value)">
<input class="btn" type="button" name="b6" value="6" onclick="func(b6.value)">
<input class="btn" type="button" name="minus" value="-" onclick="func(minus.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b1" value="1" onclick="func(b1.value)">
<input class="btn" type="button" name="b2" value="2" onclick="func(b2.value)">
<input class="btn" type="button" name="b3" value="3" onclick="func(b3.value)">
<input class="btn" type="button" name="mul" value="*" onclick="func(mul.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b0" value="0" onclick="func(b0.value)">
<input class="btn" type="button" name="bd" value="." onclick="func(bd.value)">
<input class="btn" type="button" name="divv" value="/" onclick="func(divv.value)">
<input class="equal" type="button" value="=" onclick="displayResult.value=eval(displayResult.value)">
</div>
</div>
</form>
</div>
<script>
function func(result) {
let form = document.getElementById('form')
form.displayResult.value = form.displayResult.value + result;
}
</script>
</body>

form isn't defined here. What you need to do is get the input field as an object and change it's text.
function func(result) {
let input = document.getElementById("input-field");
input.value += result;
}
Before this, add an id to your input tag: " "input-field".

It doesn't work because your <form> doesn't have an id or name attribute.
If you want to access your elements without querying the DOM like the given code, you should give your element an id or name attribute, then it becomes a property of window (a global variable) and you can directly access it from JavaScript.
function func(result) {
console.log(form);
form.displayResult.value = Number(form.displayResult.value) + Number(result);
}
<div class="container">
<form id="form" class="form">
<div class="display">
<input name="displayResult" type="text" placeholder="0" value="0" />
</div>
<div class="buttons">
<div class="row">
<input class="btn" type="button" name="b7" value="7" onclick="func(b7.value)">
<input class="btn" type="button" name="b8" value="8" onclick="func(b8.value)">
<input class="btn" type="button" name="b9" value="9" onclick="func(b9.value)">
<input class="btn" type="button" name="plus" value="+" onclick="func(plus.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b4" value="4" onclick="func(b4.value)">
<input class="btn" type="button" name="b5" value="5" onclick="func(b5.value)">
<input class="btn" type="button" name="b6" value="6" onclick="func(b6.value)">
<input class="btn" type="button" name="minus" value="-" onclick="func(minus.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b1" value="1" onclick="func(b1.value)">
<input class="btn" type="button" name="b2" value="2" onclick="func(b2.value)">
<input class="btn" type="button" name="b3" value="3" onclick="func(b3.value)">
<input class="btn" type="button" name="mul" value="*" onclick="func(mul.value)">
</div>
<div class="row">
<input class="btn" type="button" name="b0" value="0" onclick="func(b0.value)">
<input class="btn" type="button" name="bd" value="." onclick="func(bd.value)">
<input class="btn" type="button" name="divv" value="/" onclick="func(divv.value)">
<input class="equal" type="button" value="=" onclick="displayResult.value=eval(displayResult.value)">
</div>
</div>
</form>
</div>
P.S:
I'm not saying this is a good practice, just sharing how it works.
Also value attribute returns a string, you should convert them to
number in order to perform mathematical operations.
Your result input doesn't have a default value (placeholder="0" isn't the same as value="0"), you should set it as 0 or handle the empty string value in JavaScript

Related

Event listener doesnt work on some HTML elements

I have 2 groups of HTML buttons, all with the class btn.
const btn = document.querySelectorAll('.btn');
const btnArray = Array.from(btn);
btnArray.forEach((item) => {
item.addEventListener('click', () => {
console.log('clicked');
console.log(item.value)
});
});
<div class="calculator">
<div class="display">
<p class="output">YES</p>
</div>
<div class="operations">
<input type="submit" class="btn add" value='+'>
<input type="submit" class="btn subtract" value='-'>
<input type="submit" class="btn multiply" value='x'>
<input type="submit" class="btn divide" value=÷>
<input type="submit" class="btn clear" value='CLEAR'>
</div>
<div class="calcBody">
<input type="submit" class="btn seven" value='7'>
<input type="submit" class="btn eight" value='8'>
<input type="submit" class="btn nine" value='9'>
<input type="submit" class="btn four" value='4'>
<input type="submit" class="btn five" value='5'>
<input type="submit" class="btn six" value='6'>
<input type="submit" class="btn one" value='1'>
<input type="submit" class="btn two" value='2'>
<input type="submit" class="btn three" value='3'>
<input type="submit" class="btn zero" value='0'>
<input type="submit" class="btn period" value='.'>
<input type="submit" class="btn off" value='ON/OFF'>
</div>
</div>
This works only on the buttons in the calcBody div and not the ones in the operations div. Yet, when I console.log(btnArray) it shows all of the buttons (which is weird).
Can someone tell me why? Thanks.
works perfectly! I checked it on Google Chrome.

Getting values inside jquery from input fields with dynamic id

I want to get value of input fields with dynamic id to jquery function
<input type="text" id="a_8" name="a_8" value="12">
<input type="text" id="b_8" name="b_8" value="22">
<button type="button" class="btn btn-success" onclick="javascript:det(8);">Submit</button>
<input type="text" id="a_9" name="a_9" value="22">
<input type="text" id="b_9" name="b_9" value="52">
<button type="button" class="btn btn-success" onclick="javascript:det(9);">Submit</button>
Jquery function
function det(a)
{
//how can I get values of id a_8,b_8 when first button is clicked
and a_9,b_9 values when second button is clicked
}
you can simply create id of your control by appending method argument a to a_ or b_ like $("#a_"+a).val()
function det(a)
{
alert($("#a_"+ a ).val());
alert($("#b_"+ a ).val());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="a_8" name="a_8" value="12">
<input type="text" id="b_8" name="b_8" value="22">
<button type="button" class="btn btn-success" onclick="javascript:det(8);">Submit</button>
<input type="text" id="a_9" name="a_9" value="22">
<input type="text" id="b_9" name="b_9" value="52">
<button type="button" class="btn btn-success" onclick="javascript:det(9);">Submit</button>
function det(a){
var b1 = $('#a_'+a).val();
var b2 = $('#b_'+a).val();
console.log('values: '+b1+','+b2);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="a_8" name="a_8" value="12">
<input type="text" id="b_8" name="b_8" value="22">
<button type="button" class="btn btn-success" onclick="javascript:det(8);">Submit</button>
<input type="text" id="a_9" name="a_9" value="22">
<input type="text" id="b_9" name="b_9" value="52">
<button type="button" class="btn btn-success" onclick="javascript:det(9);">Submit</button>

Why is my global variable undefined inside function? [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 3 years ago.
I made var display_val = document.case.display.value; and this variable is called inside function:
run0() {
var display_val = document.case.display.value;
display_val += "0"
queue.push('0')
};
My code doesn't work and the console gives me the following error:
Uncaught TypeError: Cannot read property 'display' of undefinedat
main.js:72
js:72 is var display_val = document.case.display.value;
Full html code
<html>
<head>
<!--Copyright 2019, Aleksa Kovacevic, All rights reserved.-->
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Online calculators for everything. Some solve problems, some satisfy curiosity." />
<meta name="keywords" content="calculator, mortgage, loan,lease, cooking, math, college tuition, agriculture, finance,fractions,love,scientific, design, health, unit converter, pocket, running, calculators" />
<link rel="icon" href="https://www.apkmirror.com/wp-content/uploads/2017/11/5a0aad10ea5ec.png">
<title id= "Title">Calculator </title>
<link href="https://fonts.googleapis.com/css?family=Inconsolata&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
<link rel="stylesheet" href="main.css" type="text/css">
<link rel="stylesheet" href="Notiflix\node_modules\notiflix\dist\notiflix-1.8.0.min.css" />
<script src="Notiflix\node_modules\notiflix\dist\notiflix-aio-1.8.0.js""></script>
<script src="main.js" type="text/javascript"></script>
</head>
<body>
<button type="button" id="Git" onclick="Git()"> GitHub</button>
<div id="wrapper">
<form name="case" > <!--Buttons -->
<input name="display" id="display" placeholder "0" onkeypress="" autofocus readonly>
<input type="button" class="oper" value="(" onclick="runLB()">
<input type="button" class="oper" value=")" onclick="runRB()">
<input type="button" id="back" class="oper" value="CE" onclick="runBack()">
<input type="button" id="divide" class="oper" value="÷" onclick="runDivide()" >
<input type="button" class="digit" value="1" onclick="run1()">
<input type="button" class="digit" value="2" onclick="run2()">
<input type="button" class="digit" value="3" onclick="run3()">
<input type="button" id="multiply" class="oper" value="×" onclick="runMultiply()">
<input type="button" class="digit" value="4" onclick="run4()">
<input type="button" class="digit" value="5" onclick="run5()">
<input type="button" class="digit" value="6" onclick="run6()">
<input type="button" id="minus" class="oper" value="-" onclick="runMinus()" >
<input type="button" class="digit" value="7" onclick="run7()">
<input type="button" class="digit" value="8" onclick="run8()">
<input type="button" class="digit" value="9" onclick="run9()">
<input type="button" id="plus" class="oper" value="+" onclick="runPlus()">
<input type="button" class="digit" value="0" onclick="run0()">
<input type="button" id="comma" class="digit" value="." onclick="runComma()">
<input type="button" id="equal" class="oper" value="=" onclick="runEquals()">
<div id="Cal">
<textarea id ="TE" placeholder="Note"></textarea>
</div>
<div id="newpos">
<!-- button rainbow -->
<button type="button" id="Note" onclick="myFunction()"> Note</button></div>
</form>
<div id="new">
<!--result textarea-->
<textarea id="result" placeholder="History" readonly></textarea>
<button type="button" id="Del" onclick="Del()"> Delete</button>
<button type="button" id="Print" onclick="printTextArea()" > Print</button>
<button type="button" id="FP" onclick="FontP()" >Font +</button>
<button type="button" id="FM" onclick="FontM()" >Font -</button>
<button type="button" id="SaveBtn" onclick="SaveBtn" >Save</button>
</div>
</div>
</body>
Any informations will help me
display_val is a global variable.
To use it inside a function you have to use this.display_val instead of var

I want the value of my button to appear in a textbox upon clicking the button

I am creating an onscreen keyboard, and want a function which will allow when any of the buttons are pressed, for their values to appear in a text box to the side of the keyboard. The code I have so far is:-
<script type="text/javascript">
function onclick(){
document.getElementById("output").value
=document.getElementById("Q").value;
}
</script>
And the HTML code below:-
<div class ="Row">
<div class="Box2">
<form id="keyboard" name="keyboard">
<div>
<input type="button" onclick='onclick' id="Q" value="Q">
<input type="button" value="W">
<input type="button" value="E">
<input type="button" value="R">
<input type="button" value="T">
<input type="button" value="Y">
<input type="button" value="U">
<input type="button" value="I">
<input type="button" value="O">
<input type="button" value="P">
</div>
<div>
<input type="button" value="A">
<input type="button" value="S">
<input type="button" value="D">
<input type="button" value="F">
<input type="button" value="G">
<input type="button" value="H">
<input type="button" value="J">
<input type="button" value="K">
<input type="button" value="L">
</div>
<div>
<input type="button" value="Z">
<input type="button" value="X">
<input type="button" value="C">
<input type="button" value="V">
<input type="button" value="B">
<input type="button" value="N">
<input type="button" value="M">
</div>
<div>
<input type="button" value="SPACE">
<input type="button" value="ENTER">
</div>
</form>
<input type='text' id='output' />
</div>
</div>
</div>
You have it tagged as jQuery, so here is a more elegant jQuery solution: Throw that code away and use a single delegated jQuery event handler. Start with something like this:
$('[name=keyboard]').on('click', 'input[type=button]', function(){
var value = $(this).attr('value');
$('#output').val($('#output').val() + value);
});
JSFiddle: https://jsfiddle.net/TrueBlueAussie/kaau601u/
Obviously you need to handle the SPACE and ENTER as special cases, but you gave no clues what you are doing next, so leaving that to the reader to finish :)
Notes:
You either need to place this code after the elements it references or put it in a DOM ready handler.
Like this:
$(function(){
$('[name=keyboard]').on('click', 'input[type=button]', function(){
var value = $(this).attr('value');
$('#output').val($('#output').val() + value);
});
});
Or you can use a document attached handler, which is always present:
Like this:
$(document).on('click', 'input[type=button]', function(){
var value = $(this).attr('value');
$('#output').val($('#output').val() + value);
});
//Please try this working example
$('#Q').click(function(){
$('#output').val($(this).val());
console.log($(this).val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class ="Row">
<div class="Box2">
<form name=keyboard name="keyboard">
<div>
<input type="button" id="Q" value="Q">
<input type="button" value="W">
<input type="button" value="E">
<input type="button" value="R">
<input type="button" value="T">
<input type="button" value="Y">
<input type="button" value="U">
<input type="button" value="I">
<input type="button" value="O">
<input type="button" value="P">
</div>
<div>
<input type="button" value="A">
<input type="button" value="S">
<input type="button" value="D">
<input type="button" value="F">
<input type="button" value="G">
<input type="button" value="H">
<input type="button" value="J">
<input type="button" value="K">
<input type="button" value="L">
</div>
<div>
<input type="button" value="Z">
<input type="button" value="X">
<input type="button" value="C">
<input type="button" value="V">
<input type="button" value="B">
<input type="button" value="N">
<input type="button" value="M">
</div>
<div>
<input type="button" value="SPACE">
<input type="button" value="ENTER">
</div>
</form>
<input type='text' id='output' />
</div>
</div>
I have made a slight change in the output field type with working SPACE and ENTER
jQuery('form[name="keyboard"] input[type="button"]').click(function(){
var inpVal = jQuery(this).val();
if( inpVal == "SPACE" ){
inpVal = ' ';
}else if( inpVal == "ENTER" ){
inpVal = '\n';
}
var val = jQuery('#output').val() + inpVal;
jQuery('#output').val(val);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class ="Row">
<div class="Box2">
<form name=keyboard name="keyboard">
<div>
<input type="button" onclick='onclick' id="Q" value="Q">
<input type="button" value="W">
<input type="button" value="E">
<input type="button" value="R">
<input type="button" value="T">
<input type="button" value="Y">
<input type="button" value="U">
<input type="button" value="I">
<input type="button" value="O">
<input type="button" value="P">
</div>
<div>
<input type="button" value="A">
<input type="button" value="S">
<input type="button" value="D">
<input type="button" value="F">
<input type="button" value="G">
<input type="button" value="H">
<input type="button" value="J">
<input type="button" value="K">
<input type="button" value="L">
</div>
<div>
<input type="button" value="Z">
<input type="button" value="X">
<input type="button" value="C">
<input type="button" value="V">
<input type="button" value="B">
<input type="button" value="N">
<input type="button" value="M">
</div>
<div>
<input type="button" value="SPACE" >
<input type="button" value="ENTER">
</div>
</form>
<textarea id="output" cols="40" rows="5"></textarea>
</div>
</div>
</div>
This might help
//this this the script that makes it happen...
$('form[name=keyboard]>div>input[type=button]').click(function(){
$('#output').val($(this).val());
console.log($(this).val());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class ="Row">
<div class="Box2">
<form name="keyboard">
<div>
<input type="button" value="Q">
<input type="button" value="W">
<input type="button" value="E">
<input type="button" value="R">
<input type="button" value="T">
<input type="button" value="Y">
<input type="button" value="U">
<input type="button" value="I">
<input type="button" value="O">
<input type="button" value="P">
</div>
<div>
<input type="button" value="A">
<input type="button" value="S">
<input type="button" value="D">
<input type="button" value="F">
<input type="button" value="G">
<input type="button" value="H">
<input type="button" value="J">
<input type="button" value="K">
<input type="button" value="L">
</div>
<div>
<input type="button" value="Z">
<input type="button" value="X">
<input type="button" value="C">
<input type="button" value="V">
<input type="button" value="B">
<input type="button" value="N">
<input type="button" value="M">
</div>
<div>
<input type="button" value="SPACE">
<input type="button" value="ENTER">
</div>
</form>
<input title="keyboard" type='text' id='output' />
</div>
</div>
Using just javaScript and no need for jQuery.
This example including the use of the SPACE and ENTER keys. As a bonus I added a BACKSPACE key.
Note your output text field was changed to text area to allow for the use of the enter key.
Create a single event listener for the form. This can be done using document query selector.
Capture the click event with the query selector.
Note on the <body> tag we add the onload event handler so when the document is served up the event listener is assigned to the <form> node.
Any click inside the <form> will be tested
HTML and javaScript
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<head>
<script type="text/javascript">
var g = {};
function assignListener()
{
/*event listener for keyboard*/
g.keyboard = document.querySelector("#keyboard");
g.keyboard.addEventListener("click", addToTextBox,false);
}
function addToTextBox(e)
{
/*
with query selector the event (e) is passed along
to the function. You can examine the event and get
all kinds of useful stuff from it, like type of event, where it came from, attributes like id, class list etc.
*/
if (e.target.type == 'button')
{
switch (e.target.value)
{
case "ENTER":
document.getElementById('output').value += '\n';
break;
case "SPACE":
document.getElementById('output').value += ' ';
break;
case "BACKSPACE":
document.getElementById('output').value = document.getElementById('output').value.substr(0,document.getElementById('output').value.length-1);
break;
default:
document.getElementById('output').value += e.target.value;
}
}
}
</script>
</head>
<body onload="assignListener();">
<div class ="Row">
<div class="Box2">
<form id=keyboard>
<div>
<input type="button" value="Q">
<input type="button" value="W">
<input type="button" value="E">
<input type="button" value="R">
<input type="button" value="T">
<input type="button" value="Y">
<input type="button" value="U">
<input type="button" value="I">
<input type="button" value="O">
<input type="button" value="P">
</div>
<div>
<input type="button" value="A">
<input type="button" value="S">
<input type="button" value="D">
<input type="button" value="F">
<input type="button" value="G">
<input type="button" value="H">
<input type="button" value="J">
<input type="button" value="K">
<input type="button" value="L">
</div>
<div>
<input type="button" value="Z">
<input type="button" value="X">
<input type="button" value="C">
<input type="button" value="V">
<input type="button" value="B">
<input type="button" value="N">
<input type="button" value="M">
</div>
<div>
<input type="button" value="SPACE">
<input type="button" value="ENTER">
<input type="button" value="BACKSPACE">
</div>
</form>
<!-- <input type='text' id='output' /> -->
<textarea id="output" rows="5" cols="75"></textarea>
</div>
</div>
</body>
</html>

How to display number input using button for calculator in HTML?

When I click numbers, I always get the same result. Why? Here is my HTML code:
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<input type="button" id="myBtn" onclick="myFunction()" value="1">
<input type="button" id="myBtn" onclick="myFunction()" value="2">
<input type="button" id="myBtn" onclick="myFunction()" value="3">
<input type="button" id="myBtn" onclick="myFunction()" value="4">
<input type="button" id="myBtn" onclick="myFunction()" value="5">
<input type="button" id="myBtn" onclick="myFunction()" value="6">
<input type="button" id="myBtn" onclick="myFunction()" value="7">
<input type="button" id="myBtn" onclick="myFunction()" value="8">
<input type="button" id="myBtn" onclick="myFunction()" value="9">
<input type="button" id="myBtn" onclick="myFunction()" value="0">
<script>
function myFunction() {
var x = document.getElementById("myBtn").value;
document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>
1) Ids must be unique. Rewrite your HTML to use classes or unique ids.
2) To get value in myFunction you can simply pass this from elenment to access to clicked button.
<input type="button" id="myBtn" onclick="myFunction(this)" value="1">
....
function myFunction(button) {
var x = button.value;
document.getElementById("demo").innerHTML = x;
}
http://jsfiddle.net/0sewtjLs/
Still have the problem in case of more than 1digit number
Just concat string:
document.getElementById("demo").innerHTML += x;
http://jsfiddle.net/0sewtjLs/1/
try
<input type="button" id="myBtn" onclick="myFunction(1)" >
<input type="button" id="myBtn" onclick="myFunction(2)" >
<input type="button" id="myBtn" onclick="myFunction(3)" >
<input type="button" id="myBtn" onclick="myFunction(4)" >
<input type="button" id="myBtn" onclick="myFunction(5)" >
<input type="button" id="myBtn" onclick="myFunction(6)" >
<input type="button" id="myBtn" onclick="myFunction(7)" >
<input type="button" id="myBtn" onclick="myFunction(8)" >
<input type="button" id="myBtn" onclick="myFunction(9)" >
<input type="button" id="myBtn" onclick="myFunction(0)" >
<script>
function myFunction(x) {
document.getElementById("demo").innerHTML = x;
}
</script>
you can also keep logic only in Javascript, and removing the "onclick" on HTML.
In the code bellow, all your buttons have "myBtn" class. You can have infinite time the same class. But ID's are unique, be carefull.
In this code I get the all class "myBtn", wich return an Array.
On this array, I making a loop and add an eventListner listening clicks and calling the myFunction function.
<p id="demo"></p>
<input type="button" class="myBtn" value="1">
<input type="button" class="myBtn" value="2">
<input type="button" class="myBtn" value="3">
<input type="button" class="myBtn" value="4">
<input type="button" class="myBtn" value="5">
<input type="button" class="myBtn" value="6">
<input type="button" class="myBtn" value="7">
<input type="button" class="myBtn" value="8">
<input type="button" class="myBtn" value="9">
<input type="button" class="myBtn" value="0">
<script>
const allBtns = document.querySelectorAll('.myBtn');
allBtns.forEach( element => {
element.addEventListener('click', myFunction);
})
function myFunction(e) {
const value = e.target.value;
document.getElementById("demo").innerHTML = value;
}
</script>

Categories

Resources