Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
i don't get any value from document.getElementById(id).value

I am trying to get the value from a simple html textfield but whenever i write something and press enter no value is saved. i tried comparing the input from the textfield with a simple == but nothing works. I don't even get any value for console.log(textervalue). Does anyone know what i am doing wrong?

<div class="splitscreen" >
    <div class="left">
        <video class="input_video"></video>
        <canvas class="output_canvas" width="960" height="540" id="rand"></canvas>
    </div>

    <div class="right">
        <canvas class="chat_canvas" width="250" height="500" id="rundrand" ></canvas>
        
        <form action="/url" method="GET" id="rand_text">
        <input type="text" id ='texter' value='Guess here' autocomplete ='off'>
        </form>
        
    </div>
</div>
var read = 'car';
textervalue = document.getElementById("texter").value;

//just to test if i get a value
if (textervalue == read)
{
  canchat.fillStyle="#FFFFF";
  canchat.fillRect(0,0, canvasChat.width, canvasChat.height);
}

console.log(textervalue);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You don't get any value because you need an event listener so JS knows to pull data once the field has been filled.

In the HTML form, add a button and then use an event listener to check when the button was clicked. Then, JS will know to grab the value of the input field.

Without preventDefault()

let btn = document.getElementById('submitBtn');



btn.addEventListener('click', function() {

textervalue = document.getElementById("texter").value;

console.log(textervalue);

});
            
<div class="splitscreen" >
    <div class="left">
        <video class="input_video"></video>
        <canvas class="output_canvas" width="960" height="540" id="rand"></canvas>
    </div>

    <div class="right">
        <canvas class="chat_canvas" width="250" height="500" id="rundrand" ></canvas>
        
        <form action="/url" method="GET" id="rand_text">
        <input type="text" id ='texter' value='Guess here' autocomplete ='off'>
        <button id="submitBtn">Submit</button>
        </form>
        
    </div>
</div>

With preventdefault()

let btn = document.getElementById('submitBtn');



btn.addEventListener('click', function(e) {

e.preventDefault();

textervalue = document.getElementById("texter").value;

console.log(textervalue);

});
            
<div class="splitscreen" >
    <div class="left">
        <video class="input_video"></video>
        <canvas class="output_canvas" width="960" height="540" id="rand"></canvas>
    </div>

    <div class="right">
        <canvas class="chat_canvas" width="250" height="500" id="rundrand" ></canvas>
        
        <form action="/url" method="GET" id="rand_text">
        <input type="text" id ='texter' value='Guess here' autocomplete ='off'>
        <button id="submitBtn">Submit</button>
        </form>
        
    </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

<form action="/url" method="GET" id="rand_text">
        <input type="submit" onclick="fonctionHere(this)" id ='texter' value='Guess here' autocomplete ='off'>
        </form>




function fonctionHere(e) {
console.log(e.value);
}

submit is form action input directly.. and with an onclick, we have "this" it something like this same codes "getElementById" was send to the function directly

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda