I will explain my problem as best as I can. So first of all, I wrote a calculator in HTML, CSS and JavaScript, but I did the JavaScript part in the HTML file.
The JavaScript file is used intern, in HTML. So now I want to use it in an external JavaScript file, so I created one. I cant get it to work, that myFunction1() works in the external JavaScript file, so what am I doing wrong?
Let's have a look in my code:
document.getElementById("num1").addEventListener("click", myFunction1);
//I call the id num1 and tell him, if it gets clicked move on to myFunction1
function myFunction1() {
document.getElementById("num1").onClick = "document.calc.txt.value +='1'";
//Now I want that if it gets clicked, the calculator should put the number 1 on the screen, so I can remove the intern JavaScript in HTML out and just use the extern JavaScript file.
}
/*@import url('https://fonts.googleapis.com/css?family=Poppins: 300,400,500,600,700,800,900&display=swap'); */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #091921;
}
.calculator {
position: relative;
display: grid;
}
.calculator .value {
grid-column: span 4;
height: 100px;
text-align: right;
border: none;
outline: none;
padding: 10px;
font-size: 18px;
}
.calculator span {
display: grid;
width: 60px;
height: 60px;
color: #fff;
background: #0c2835;
place-items: center;
border: 1px solid rgba(0, 0, 0, .1)
}
.calculator span:active {
background: #74ff3b;
color: #111;
}
.calculator span.clear {
grid-column: span 2;
width: 120px;
background: #ff3077;
}
.calculator span.plus {
grid-row: span 2;
height: 120px;
}
.calculator span.equal {
background: #03b1ff;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="Taschenrechner.css">
<title>Calculator</title>
</head>
<body>
<form class="calculator" name="calc">
<input class="value" type="text" name="txt" readonly="">
<span class="num clear" onClick="document.calc.txt.value =''">c</span>
<span class="num" onClick="document.calc.txt.value +='/'">/</span>
<span class="num" onClick="document.calc.txt.value +='*'">*</span>
<span class="num" onClick="document.calc.txt.value +='7'">7</span>
<span class="num" onClick="document.calc.txt.value +='8'">8</span>
<span class="num" onClick="document.calc.txt.value +='9'">9</span>
<span class="num" onClick="document.calc.txt.value +='-'">-</span>
<span class="num" onClick="document.calc.txt.value +='4'">4</span>
<span class="num" onClick="document.calc.txt.value +='5'">5</span>
<span class="num" onClick="document.calc.txt.value +='6'">6</span>
<span class="num plus" onClick="document.calc.txt.value +='+'">+</span>
<span class="num" onClick="document.calc.txt.value +='3'">3</span>
<span class="num" onClick="document.calc.txt.value +='2'">2</span>
<span id="num1" class="num" onClick="document.calc.txt.value +='1'">1</span>
<!-- I used this id for JavaScript, just focus on that -->
<span class="num" onClick="document.calc.txt.value +='0'">0</span>
<span class="num" onClick="document.calc.txt.value +='00'">00</span>
<span class="num" onClick="document.calc.txt.value +='.'">.</span>
<span class="num equal" onClick="document.calc.txt.value =eval(calc.txt.value)">=</span>
</form>
<button id="demo">>Test</button>
<script type="text/javascript" src="Taschenrechner.js"></script>
</body>
</html>
To sum up:
In my HTML file are intern JavaScript codes, I used onClick="document.calc.txt.value +='/'" and that is the only thing I want to export to the extern JavaScript file. I can't get it to work, so that is my only question:
How can I transport the onClick="document.calc.txt.value +='/'" code to my extern JavaScript file, so that it works?
Thanks for your replies.