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

115
Vistas
Changing a box with buttons

I am very new to coding and am trying to combine html, css, and javascript for the first time. The task is to have buttons make changes to a box. For example one of my buttons is "grow", in which I want the box to increase in size when the button is clicked. Here is my HTML

<!DOCTYPE html>
<html>
<head>
    <title>Watch That Box</title>
    <!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> -->
</head>
<body>

    <p>Press the buttons to change the box!</p>

    <div id="box" style="height:150px; width:150px; background-color:orange; margin:25px"></div>

    <button id="button1">Grow</button>
    <button id="button2">Blue</button>
    <button id="button3">Fade</button>
    <button id="button4">Reset</button>

    <link rel="stylesheet" href="./style.css">

    <script type="text/javascript" src="javascript.js"></script>

</body>
</html>

Here is my CSS

body {
    background-color: aliceblue;
}

.box {
    height:150px; 
    width:150px; 
    background-color:orange; 
    margin:25px
}

And no javascript yet. I don't really know where to even start, I know how to combine CSS and HTML, however I am stumped when it comes to adding in the javascript. Any help is appreciated and thank you in advance.

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

0

Here is a good place to start. https://www.w3schools.com/jsref/event_onclick.asp

about 4 years ago · Juan Pablo Isaza Denunciar

0

In your button, you add a function 'onclick', like this:

<button id="button1" onclick="growFunction()">Grow</button>

Then, you go to javascript.js and create the growFunction() that will be executed when the button get clicked.

Also, to do the growFunction, either you use no parameters and inside the function you use document.getElementById('button').{your code}, or you pass this parameter, something like:

<button onclick="growFunction(this)">Grow</button>

javascript.js

growFunction(element){
    element.yourcode...
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

okay. javaScript can modify elements (HTML buttons for example) and their properties, like text content AND style (CSS). first of all, you need to select the element into a variable

let btn = document.querySelector('#grow-btn)

let btn means you're declaring a variable, piece of storage, named btn. you're putting inside of it the resulte of the expression.

btn.addEventListener("click", function(){/*here you're handling the click*/})

add a listener to the btn, so you can know when a click was made and respond to it.

inside the block you can change some css properties of a box in a similar way

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