I have a ploblema in my JS code

Asked

Viewed 77 times

-2

I created an input in HTML, but I need to put the value of this input in a variable. Yes, I have tried using queryselector the getelementbyid and it didn’t work.

I want to make a speed system and only need this variable if you see any more error in my code warn Please.

  var b = document.getElementById("botao")

  var v1 = document.getElementsByid('num1')

  var v2 = document.getElementsByName("n2")

  function clicar() {
    if (v1 >= v2) {
      alert("você estar com uma velocidade alta ")
    } else {
      alert("voce estar com uma velocidade normal")
    }
  }
    section {
      background-color: rgb(88, 230, 255);
      border-radius: 10px;
      padding: 10px;
      background-clip: initial;
      background-position: 30px;
      font-size: large;
    }
    
    body {
      background-color: aliceblue;
    }
    
    h1 {
      text-align: center;
      text-shadow: 20px;
      scrollbar-shadow-color: rgb(151, 255, 233);
    }
    
    h2 {
      border-bottom-color: aliceblue;
      color: rgb(255, 136, 136);
      text-align: center;
      font: x-small;
      font-family: 'Courier New', Courier, monospace;
    }
    
    input#button {
      font-style: oblique;
      background-color: green;
    }
  <h1><strong> Bem vindo ao sistema de velocidade do detran</strong></h1>

  <section>
    <div id="n1">
      <input type="number" name="n1" id="num1"> Qual é o limite máximo de velocidade dessa área?
    </div>
    <input type="number" name="n2" id="num2"> Qual é a sua velocidade?
  </section>
  <input type="button" value="enviar" onclick="clicar()">

  <h2>obrigado por testar</h2>

  • On the line var v1 = document.getElementsByid('num1') is spelled wrong. The correct is var v1 = document.getElementById('num1')

  • Thank you, I’ll run this and see if it works

  • Ñ worked the ploblema in the code and that qnd puts the speed he never says if only says Else,even with the null value he ignores

  • The correct is var v1 = document.getElementById('num1').value; and var v2 = document.getElementsByName("n2").value; lacked to use the property value.

  • All right, thank you very much and custom of converas via instant chat

  • It was not a scolding, Today I am an old man, but I started programming as a child and I was very humiliated and missed many opportunities for not knowing how to communicate with adults at the time.

  • Still thanks, about the code I think there’s something wrong with it put the . value;at the end of the two I don’t know what’s going on,

  • Do var v2 = document.getElementsByName("n2")[0].value; The method getElementsByName() returns an array. Now it works.

  • Only in v2? Or in v1 tb?

  • Only the v2. That’s where he used the method getElementsByName()

  • Thanks for Juliet more.... it didn’t work! Let it go I’ll use the old method by the prompts.

  • Here worked.

  • @Josepaulo when asking a question is important to be clear in the title and description, be cautious in spelling and grammar, as well as format the code properly. This makes it much easier to get help.

  • Only missing comment saying that the variables should be within the Function and that the parole is changed

  • @Was Josepaulo able to verify the answers presented? But forget to accept the one you find most suitable and put in all you find good.

Show 10 more comments

2 answers

3

There are some problems with your code. Some are even problems, which do not allow the code to work properly, others only hinder the reading and construction of the code.

ex:

  • var b = document.getElementById("botao") if b is not used, because have this line?
  • v1 and v2 are similar. Why use getElementsByid and getElementsByName on the other?
  • There is a div with id n1 and an input where the name has the same value.
  • Element and variable names are not "self-explanatory".
  • The input value is read outside the function and the variables are with the value when Javascript is interpreted by the browser.

function clicar() {
  var velocidadeMax = document.getElementById('velocidadeMax').value;
  var velocidade = document.getElementById("velocidade").value;

  if (velocidadeMax < velocidade) {
    alert("Velocidade está acima do permitido.");
  } else {
    alert("Velocidade está dentro do permitido.");
  }
}
    section {
      background-color: rgb(88, 230, 255);
      border-radius: 10px;
      padding: 10px;
      background-clip: initial;
      background-position: 30px;
      font-size: large;
    }
    
    body {
      background-color: aliceblue;
    }
    
    h1 {
      text-align: center;
      text-shadow: 20px;
      scrollbar-shadow-color: rgb(151, 255, 233);
    }
    
    h2 {
      border-bottom-color: aliceblue;
      color: rgb(255, 136, 136);
      text-align: center;
      font: x-small;
      font-family: 'Courier New', Courier, monospace;
    }
    
    input#button {
      font-style: oblique;
      background-color: green;
    }
<h1><strong> Bem vindo ao sistema de velocidade do detran</strong></h1>
<section>
  Qual é o limite máximo de velocidade dessa área? <input type="number" id="velocidadeMax"><br />
  Qual é a sua velocidade? <input type="number" id="velocidade">
</section>
<input type="button" value="enviar" onclick="clicar()">

<h2>obrigado por testar</h2>

1

Wrong var v1 = document.getElementsByid('num1')

The name of this method must be correct getElementById

For example, getElementByID does not work, however natural it may seem.

the Document.getElementById('num1') method returns the elemento which has the attribute id = num1

var v1 = document.getElementById('num1');
  console.log(v1);
<input type="number" name="n1" id="num1">

This method is one of the most common methods in HTML DOM and is used almost every time you want to manipulate or get information from an element in your document.

To refer to the field value use the property value

var v1 = document.getElementById('num1').value;
  console.log(v1);
<input type="number" name="n1" id="num1" readonly value=50>

The syntax is correct var v2 = document.getElementsByName("n2") however, the getElementsByName() method returns a collection of all elements in the document with the specified name (the value of the name attribute) as an object.

The object represents a collection of nodes. Nodes can be accessed by index numbers. The index starts at 0.

To refer to the field value use the property value

  var v2 = document.getElementsByName("n2")[0].value;
  console.log(v2);
  var v3 = document.getElementsByName("n2")[1].value;
  console.log(v3);
<input type="number" name="n2" readonly value=20>
<input type="number" name="n2" readonly value=33>

NOTE: without the index the returned value is undefined even if it has only one element

      var v2 = document.getElementsByName("n2").value;
      console.log(v2);
 <input type="number" name="n2" readonly value=20>

This variable var b = document.getElementById("botao") does not interfere in the execution of the script and is there of silly.

Applying the above we have:

  function clicar() {
  
  //essas variaveis serão recuperadas ao chamar a função portanto devem estar dentro da função
  var v1 = document.getElementById('num1').value;
  var v2 = document.getElementsByName("n2")[0].value;

    if (v1 >= v2) {
       //se v1 for maior ou igual que v2 velocidade normal
      //alert("você estar com uma velocidade alta ");
      alert("você estar com uma velocidade normal ")
    } else {
      //alert("voce estar com uma velocidade normal")
      alert("voce estar com uma velocidade alta");
    }
  }
section {
      background-color: rgb(88, 230, 255);
      border-radius: 10px;
      padding: 10px;
      background-clip: initial;
      background-position: 30px;
      font-size: large;
    }
    
    body {
      background-color: aliceblue;
    }
    
    h1 {
      text-align: center;
      text-shadow: 20px;
      scrollbar-shadow-color: rgb(151, 255, 233);
    }
    
    h2 {
      border-bottom-color: aliceblue;
      color: rgb(255, 136, 136);
      text-align: center;
      font: x-small;
      font-family: 'Courier New', Courier, monospace;
    }
    
    input#button {
      font-style: oblique;
      background-color: green;
    }
<h1><strong> Bem vindo ao sistema de velocidade do detran</strong></h1>

  <section>
    <div id="n1">
      <input type="number" name="n1" id="num1"> Qual é o limite máximo de velocidade dessa área?
    </div>
    <input type="number" name="n2" id="num2"> Qual é a sua velocidade?
  </section>
  <input type="button" value="enviar" onclick="clicar()">

  <h2>obrigado por testar</h2>

Browser other questions tagged

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