I got a JavaScript file and a bootstrap form from the internet. On the page there are 2 inputs, when you fill them in it will add the numbers together and put the new number out.
When I tried it out it didn't do anything, in the console I got 2 errors. First one being that bootstraps JavaScript needed jQuery so I added that, and the second one says "Uncaught TypeError: Cannot read property 'addEventListener' of null", and I can't figure out why I get this error.
The form code is:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="test.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container text-center">
<h1>
test title
</h1>
<form action="#" class="form-inline">
<input type="text" id="first"class="form-control" placeholder="Enter your first number">
<input type="text" id="second" class="form-control" placeholder="Enter your second number">
</form>
<h2 id="result"></h2>
</div>
</body>
</html>
The JavaScript code is:
var first = document.getElementById('first');
var second = document.getElementById('second');
var result = document.getElementById('result');
first.addEventListener("input", sum);
second.addEventListener("input", sum);
function sum() {
var one = parseFloat(first.value) || 0;
var two = parseFloat(second.value) || 0;
var add = one+two;
result.innerHTML = "Your Sum is : " + add;
}
So is there maybe anyone who can explain what I'm messing up here?