I am working on my node webserver and cannot get addEventListener to work with a button click for my website. I have simulated it on JS fiddle and it works perfectly fine!
https://jsfiddle.net/dxfqj4ys/2/
I am now trying to implement exact same thing for my actual website which i running on A Raspberry PI node.
My index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('/');
socket.on('stats', function(data) {
console.log('Connected clients:', data.numClients);
});
document.addEventListener('click', function(){
document.getElementById("demo").innerHTML = "Hello World!";
});
var button = document.getElementById("button1");
button.addEventListener("click", function(event){
document.getElementById('number1').textContent = 50;
});
</script>
<!DOCTYPE html>
<html>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<div>
<input id="button1" type="button" value="Add">
</div>
<br>
<div class="custom-select" style="width:200px;">
<select>
<option value="0">Select operation:</option>
<option value="1">RUT240</option>
<option value="2">FMB920</option>
</select>
</div>
<br>
<table id="t1">
<caption>Operation table</caption>
<thead>
<tr>
<th>Operation code</th>
<th>To Do</th>
<th>Done</th>
<th>Left</th>
</tr>
</thead>
<tbody>
<tr>
<td>RUT240</td>
<td>1000</td>
<td>50</td>
<td>
</td>
</tr>
<tr>
<td>FMB920</td>
<td>555</td>
<td>50</td>
<td id ="number1">
</td>
</tr>
</tbody>
</table>
<p id="demo"></p>
</html>
And my webserver.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var request = require('request');
//handles get request and serves index.html
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
server.listen(8080);
//THE FOLLOWING WILL COUNT THE NUMBERE OF CLIENTS
var numClients = 0;
io.on('connection', function(socket)
{
numClients++;
io.emit('stats', { numClients: numClients });
console.log('Connected clients:', numClients);
socket.on('disconnect', function()
{
numClients--;
io.emit('stats', { numClients: numClients });
console.log('Connected clients:', numClients);
});
});
As you can see from my .html, I have 2 event listeners, One is modifying element named "demo" and the other one is supposed to modify element with ID "number1".
The element with "demo" works fine, however, the "number1" does not work.
When I connect to the server from another computer as a client, open a developer menu and I can see the following error:
Uncaught TypeError: Cannot read property 'addEventListener' of null