Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a little new to JavaScript, i'm having trouble trying to enlarge or make a Box grow just by pressing a button, or to change the color of the box into a different color using JavaScript. For example, to expand the image of the box each time the button labeled grow is pressed. I am wondering on how to link the buttons into the image of the box. that goes for the coloring of the box as well. Please help, much appreciated. Thank you.




Here are my HTML codes for my webpage that I am trying to enhance using Javascript:

<html>
<head>
    <title>Jiggle Into JavaScript</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>

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

</body>
</html>


What I have tried:

I have tried a couple of things but I am very inexperienced in JavaScript which made this task more challenging. Please help. Thank You
Posted
Updated 2-Jan-17 17:10pm
v4
Comments
David_Wimbley 2-Jan-17 20:36pm    
Post your javascript and HTML snippets you have tried so we can see what route you have chosen to go with your code. Javascript has a ridiculous number of frameworks and if i post code from framework XYZ that isn't going to be of much help for you when you were looking for say...jQuery.
Member 12931605 2-Jan-17 21:04pm    
My JavaScript coding is completely incorrect and I am certain that I've input in the wrong codes but this is what I have so far: var content = document.getElementsById("button")
var button = document.getElementsById("grow")
button.onclick = function changesize() {
var size= document.getElementsById("box");

Thank you for your response.

P.S. I've also added my HTML Coding into my question, I hope that helps and clarify's my question. Thanks again

1 solution

Take a look at the demo code using jQuery-a JavaScript library, have some fun while learning:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){

    $("#grow").click(function(){
        $("#myImg").animate({width: '+=20px'});
    });

});
</script>
</head>
<body>

<button id="grow">Zoom In!</button><br>

<img id='myImg' src='https://s-media-cache-ak0.pinimg.com/736x/d7/9e/b8/d79eb82db960906a3da0439ea5be40d5.jpg'>

</body>
</html>

Try it at JSFiddle[^]
Adapt and learn.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900