-2
I wanted to create a system that asked the nationality of the user and if he answered "Brasileiro"
, the if
would lead to a response. Should any other nationality, the else
would take you to another, at least that was my goal. However, the message does not appear.
You know how you can help me?
function clica() {
var r = window.document.getElementById('nac')
var r1 = window.document.getElementById('div#res')
var r2 = (r.value)
if (r2 == 'Brasileiro') {
r1.innerHTML = `Olá, você é ${r}, certo?`
} else {
r1.innerHTML = 'Hi, what language do you speak?'
}
}
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sistema de Identificação de Nacionalidade</title>
<style>
body {
background-color: grey;
}
</style>
</head>
<body>
<h1>SISTEMA DE IDENTIFICAÇÃO</h1>
Qual sua nacionalidade? <input type="text" id="nac">
<input type="button" value="Clique" onclick="clica()">
<div id='res'>RESULTADO:</div>
</body>
</html>
As Javascript is "Case Sensitive" you will soon have problems if someone puts "Brazilian" or "BRAZILIAN" (uppercase / minusculas and etc) then in "if R2" put "if (R2.toLowerCase() == 'Brazilian')". The toLowerCase function turns everything into minute
– Marcelo