The 'addEventListener' property of null can not be read

1

When I execute my code, in the console it returns me:

  

"Uncaught TypeError: Can not read property 'addEventListener' of null"

function calcularPromedio(json) {
  var out = "-----------Calcultar Nota Promedio----------<br>";
  var i;
  var acumulador = 0.0;
  for (i = 0; i < json.length; i++) {
    acumulador += json[i].nota;
  }

  var promedio = acumulador / json.length;

  alert("La nota promedio es: " + promedio);

}

document.getElementById("boton1").addEventListener("click", function() {
  calcularPromedio(estudiantes);
});
<button id="notaPromedio">Mostrar Nota Promedio</button>

What am I doing wrong and how can I correct it?

    
asked by Gustavo 04.05.2017 в 15:52
source

1 answer

1

You do not have any html element with the attribute id called button1 , you have a button with the attribute id called notaEnedio therefore you should change your code as follows:

function calcularPromedio(json) {
  var out = "-----------Calcultar Nota Promedio----------<br>";
  var i;
  var acumulador = 0.0;
  for (i = 0; i < json.length; i++) {
    acumulador += json[i].nota;
  }

  var promedio = acumulador / json.length;

  alert("La nota promedio es: " + promedio);

}

// cambia boton1 por notaPromedio en el selector
document.getElementById("notaPromedio").addEventListener("click", function() {
  calcularPromedio(estudiantes);
});
<button id="notaPromedio">Mostrar Nota Promedio</button>
    
answered by 04.05.2017 / 15:58
source