What happens there is that you have none else
in your code, and you FOUND that if I didn’t fall in if (who is also wrong) would execute the other code as an Else, BUT NOT, whenever executing the function independent of if it is true
or not the innerHTML that shows the voter data will always be executed because it is not in any condition. To solve there are 2 ways, or, puts the code with the voter data inside an Else or inserts a return
no if, what will stop the execution of the function there.
Your if is also wrong, missed check if the input txt_tEleitor
is empty, the way you just input txt_mVotacao
is being checked for value or is empty. Another error is that you used the comparison operator &&
, i.e., to return what is inside if the 2 inputs would have to be empty, which is not correct, if an input is already empty you should already show the message to fill the fields:
function liberarUrna(){
txt_tEleitor = document.querySelector('input#txt_tEleitor');
txt_mVotacao = document.querySelector('input#txt_mVotacao');
tEleitor = txt_tEleitor.value;
mVotacao = txt_mVotacao.value;
result = document.querySelector('div#result')
if (tEleitor == '' || mVotacao == '') {
return result.innerHTML = `<p>Favor preencher os campos</p>`
}
result.innerHTML = `<p>Titulo de eleitor: ${tEleitor} <br> Município: ${mVotacao} <br><strong>Urna liberada para votação!</p>`
}
<input type="text" id="txt_tEleitor">
<input type="text" id="txt_mVotacao">
<button onclick="liberarUrna()">Votar</button>
<div id="result"></div>
OBS 1: As stated in Luiz’s comment, you do not need to convert the input value to string, because the value returned by the inputs are already compatible with the string type.
OBS: 2 No need to declare the window object before Document, because window is already the root element of a web page, so in the DOM tree structure Document already belongs to window.
What is the need of the builder
String
there, once the propertyvalue
already returns a string (technically,DOMString
, but has full compatibility with the primitivestring
). Besides, I could try [Edit] your question to add a [mcve]? Try to add, in addition to Javascript, which is already present in the question, HTML in snippet code (provided by the text editor itself) so that we can reproduce your problem.– Luiz Felipe
thank you so much for your help, Luiz! I will check it out as soon as possible.
– Carlos Eduardo