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

96
Vistas
How do I make variables change outside functions in javascript?

I am a beginner with Javascript/programming and I am trying to make the "demo2" id change with the 5, 10 or 15 variable chosen in the functions (trigger by the buttons), but it keeps showing "0". What do I have to do?

    <!DOCTYPE html>
    <html>
    
    <body>
      <button onclick="alternative1()">5</button>
        <button onclick="alternative2()">10</button>
          <button onclick="alternative3()">15</button>
          
          <p id="demo"></p>
          <p id="demo2"></p>
    
      <script>
       
       var x = 0;
       var y = 0;
      
    
        function alternative1() {y = x + 5;
          
    document.getElementById("demo").innerHTML = "You have chosen " + y;
        } 
        
        
        function alternative2() {y = x + 10;
    document.getElementById("demo").innerHTML = "You have chosen "+ y;
        }
        
        
        function alternative3() {y = x + 15;
          
    document.getElementById("demo").innerHTML = "You have chosen "+ y;
        }
        
        document.getElementById("demo2").innerHTML = y;
        
      </script>
    </body>
    </html> 

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

You can’t access variables declared inside a function from outside a function. The variable belongs to the function’s scope only, not the global scope.

about 4 years ago · Santiago Gelvez Denunciar

0

You don't need 3 functions for this. You can do it with 1.

    <!DOCTYPE html>
    <html>
    
    <body>
      <button onclick="alternative(this)">5</button>
        <button onclick="alternative(this)">10</button>
          <button onclick="alternative(this)">15</button>
          
          <p id="demo"></p>
    
      <script>
        function alternative(obj) {          
    document.getElementById("demo").innerHTML = "You have chosen " + obj.innerHTML;
        }
      </script>
    </body>
    </html> 

Happy learning

about 4 years ago · Santiago Gelvez Denunciar

0

It is normal that there is zero because in 'y' there is zero and no operation performed on y.

I hope that this answer will help you to understand what you have done.

about 4 years ago · Santiago Gelvez 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