0
I am using the view engine handlebars, however, when I went to manipulate DOM elements, it continues with the value given to it by the exhaust, follows the code below:
monitor page.handlebars:
<script src="/monitoramento.js"></script>
{{#each result}}
<div class="container mt-5">
<h6>Bomba 1: <small id="bomba1">{{bomba}}</small></h6>
<hr>
<h6>Bomba 2: <small id="bomba2">texto aqui</small></h6>
<hr>
<h6>Caixa D'Água: <small id="caixa">texto aqui</small></h6>
<hr>
<h6>Cisterna: <small id="cisterna">texto aqui</small></h6>
<hr>
<h6>Temperatura: <small id="temperatura">texto aqui</small></h6>
<hr>
<h6>Pressão: <small id="pressao">texto aqui</small></h6>
<hr>
<h6>Data: <small id="data">texto aqui</small></h6>
<hr>
</div>
{{else}}
<h1>Não foram encontrados registros!</h1>
{{/each}}
Small script monitoring.js for manipulating DOM elements:
var bomba1 = document.querySelector('#bomba1')
if (bomba1.value = 1) {
bomba1.innerHTML = 'Bomba ligada'
}
why doesn’t it work?
NOTE: In the root file of the project I put the path to the static contents.
In the
if
you are using a wrong operator onbomba1.value = 1
, when it should be==
or===
. Behold in this topic the difference between them.– Sam
Your if is assigning '1' instead of comparing if value is equal to '1'. Use operator '==' or else the same strict operator '==='.
– Mateus Daniel
I didn’t even realize this mistake I made I will implement here and I already give the answer...
– Teuuz1994
I tried to implement here, but continues to give the same result, it does not replace the element :/
– Teuuz1994
Look on the console for possible errors.
– Sam
in the browser console shows the following error: monitoring.js:3 Uncaught Typeerror: Cannot read Property 'value' of null don’t know why but ta giving value "null"
– Teuuz1994
You don’t seem to have set VALUE for the element with id "bomba1". Try putting a VALUE on it, which I think your IF will work.
– Gato de Schrödinger
It is because vc is loading the monitoring file before the elements. When you try to run the line
var bomba1 = document.querySelector('#bomba1')
the element#bomba1
doesn’t exist yet.– Sam
i imported the script below any code and set VALUE for the bomba1 variable, even though it didn’t work I left the variable so:
var bomba1 = document.querySelector('#bomba1').value
– Teuuz1994
in the
if
I left so:if (bomba1.value == 1) {
 bomba1.innerHTML = 'Bomba ligada'
}
– Teuuz1994
@Teuuz1994 you did the
compile
in the Handlebars?– Mateus Daniel
I haven’t used handlebars in a while, I even looked at the documentation but I didn’t understand it very well
– Teuuz1994