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

84
Vistas
Problem with the function of pressing a button in JavaScript

I set a button with a function and it gives me an error:

end1.html: "20 Uncaught ReferenceError: reload is not defined at HTMLButtonElement.onclick"

How do I fix this?

<body>
    <button id="Reload" type="button" class="myButton" onclick="reload()"> reload </button>

<script>
function reload(){
    if(localStorage.a==6){
        Location.href="page2.html";
    }
    if(localStorage.a==8){
        Location.href="page3.html";
    }
    if(localStorage.a==10){
        Location.href="page4.html";
    } 
 } 
</script>
</body>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

A fix is to use the built-in addEventListener() function in JavaScript, instead of using the onclick attribute in HTML.

To use this feature, you can delete the onclick attribute on your <button> element in your HTML, so that it will look like this.

<button id="Reload" type="button" class="myButton">Reload</button>

Then, you can add the following function in your JavaScript. Add the following lines below your reload() function.

const reloadButton = document.querySelector("#Reload"); // Get the button from the DOM

function reload() {
  // Function code...
}

reloadButton.addEventListener("click", reload); // Alternative to 'onclick'

The listener will wait for a click event, and when it occurs, it will run the reload() function (without a ReferenceError).


Also, in your reload() function, you need to change Location to location. This is because JavaScript is case-sensitive, and it will throw an error if you leave Location as-is.

// Before
Location.href;

// After
location.href;
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