I made a code and I cannot identify the error. I am new in the area of programming

Asked

Viewed 42 times

-1

The 'program' is this:

function verificar() {
  var país = document.getElementById('querochora')
  var res = document.getElementById('res')
  if (país != 'Brasil') {
    res.innerHTML = 'Você é estrangeiro.'
  } else {
    res.innerHTML = 'Você é <strong>Brasileiro</strong>.'
  }
}
<!DOCTYPE html>
<html lang="pt-br">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>VERIFICADOR DE PAÍS.</title>
</head>

<body>
  <h1>Digite abaixo o seu país.</h1>
  Seu país: <input type="text" name="país" id="país">
  <input type="button" value="Verificar" onclick='verificar()'>
  <div id='res'>

  </div>

</body>

</html>

For some reason he’s wrong, someone can tell why?

  • 1

    Test like this if (país.value != 'Brasil')

  • 1

    you have to exchange the id of the element in javascript for país. For the element with id querochora does not exist in your html. In addition to setting @Augustovasques

  • Thanks kkk You can explain to me what this means (.value)?

  • 1

    Also try to explain the problem, not only say you have a mistake, so make life easier for those who are trying to help.

  • And avoid using accents and special characters now! Whether for variable names or filenames!!

  • The reason to use the .value is that document.getElementById te returns an HTML element, an input, in the case. The input has a property called value, which is where the "value" of the input is.

  • I would also say that the question should be closed as a "typo", since the problem is changing the name of the element...

  • O . worth will take the value of the field, in case your field is pais and it will take the value of this field (input).

Show 3 more comments

1 answer

1

Hi, come on:

First: the id that is picking up the var pais = Document.getElementById('querochora') in HTML does not exist;

Second: if you change the id from 'querochora' to 'parents' the.value will take the value typed in the html input.

In case it would be so your if:

var país = document.getElementById('pais')
if (pais.value != 'Brasil') {
...
}

and its html:

Seu país: <input type="text" name="país" id="país">

Another point is that in a perfect scenario the user would type the B of the larger Brazil, I suggest adding a country.value.toLowerCase() of the JS to pick up the text inserted by the user in low box (famous minuscule).

View the Mozila documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase

Browser other questions tagged

You are not signed in. Login or sign up in order to post.