Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
.Value is giving a console error in javascript

I am fairly new to programming. I am fiddling around with small projects to get some experience. While I was coding, I encountered a problem. I am going to write the basic code down below. When I run the code, the console gives this error "Cannot read properties of null (reading 'value')"

function startFunction() {
  let string = document.getElementById("inputbutton").value;
  console.log(string);
}
<input id="text" placeholder="Type Something" maxlengh="50">
<button id="inputbutton " onclick="startFunction()">Submit</button>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You have selected the wrong element. I believe you're trying to select the

    <input>

element not the

  <button>

element. If you change

  let string = document.getElementById("inputbutton").value;

to

  let string = document.getElementById("text").value;

and it should work. The reason is that the button element doesn't support the value property. Check on w3schools' HTML DOM element table for more information. Here is the fixed code with runnable example:

function startFunction() {
  let string = document.getElementById("text").value;
  console.log(string);   
}
<input id="text" placeholder="Type Something" maxlengh="50">
<button id="inputbutton "onclick="startFunction()">Submit</button>

Here are few things to remember:

  1. Do not load the script before the document. For example, the following code will give you an error:
<script>
document.getElementById('d').innerHTML = "Hello";
</script>
<div id="d">
</div>
  1. To fix such problem, put the script after the HTML element you're trying to work with. The following code will not throw an error:
<div id="d">
</div>
<script>
document.getElementById('d').innerHTML = "Hello";
</script>

This is because the browser execute code line by line in the same order as it is written. For the first example, the script is executed before the

<div>

element is fully loaded. So, when the browser encounters the code, it can not find the

<div>

element by ID.

about 4 years ago · Juan Pablo Isaza Denunciar

0

if you want to get the value of your input you can do this

 <input id="text" placeholder="Type Something" maxlengh="50" />
    <button id="inputbutton " onclick="startFunction()">Submit</button>

    <script>
      const textInput = document.getElementById('text')
      function startFunction() {
        let string = textInput.value
        console.log(string)
      }
     
    </script>

you can also use addEventListener

<input id="text" placeholder="Type Something" maxlengh="50" />
    <button id="inputbutton">Submit</button>

    <script>
      const textInput = document.getElementById('text')
      const button = document.getElementById('inputbutton')
      button.addEventListner('click',function(){
       let string = textInput.value
        console.log(string)
       })
     
    </script>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda