I start my coding bootcamp tomorrow and I have been able to complete all my pre-work modules except this one.
Using only html and Javascript, I am to try to get these buttons to work to change this object in the following code:
<!DOCTYPE html>
<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>
Here is where I am at, and I am obviously having issues. I feel like I know just enough to make a mess.
//the following is the fade function,
//currently unattached to button:
//source: https://stackoverflow.com/questions/28662893/fade-in-and-fade-out-in-pure-javascript-without-jquery
var box = document.getElementById('box');
function fadeOut(elem, speed) {
if (!elem.style.opacity) {
elem.style.opacity = 1;
} // end if
var outInterval = setInterval(function() {
elem.style.opacity -= 0.02;
if (elem.style.opacity <= 0) {
clearInterval(outInterval);
} // end if
}, speed / 50);
}
fadeOut(box, 2000);
// end fadeOut()
<!DOCTYPE html>
<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="growBtn">Grow</button>
<button id="blueBtn">Blue</button>
<button id="fadeBtn">Fade</button>
<button id="resetBtn">Reset</button>
<script type="text/javascript">
document.getElementById("growBtn").addEventListener("click", function() {
document.getElementById("box").style = "height:250px; width:250px;background-color: orange;margin: 25px";
});
document.getElementById("blueBtn").addEventListener("click", function() {
document.getElementById("box").style = "height:150px; width:150px;background-color: blue;margin: 25px";
});
document.getElementById("fadeBtn").addEventListener("click", function() {
document.getElementById("box").onclick.fadeOut();
});
document.getElementById("resetBtn").addEventListener("click", function() {
document.getElementById("box").style = "height:150px; width:150px;background-color: orange;margin: 25px";
});
</script>
<!-- Linking the JavaScript file -->
</body>
</html>
I have been researching for days but have found so many different options and none seem to be the right fit.
Here are the issues as they stand right now:
Grow button: Well, I can make it get bigger, but just once, not every time the button is clicked.
Blue button: I can make it blue when the button is clicked, but the size changes back to the original if the grow button was clicked first.
Fade button: I found the code to a fadeOut function on this site (referenced in the code), but I don't know how to apply it to my fadeBtn so the box fades immediately upon opening the page. This is currently the only code in my js file.
Reset button: This works! Not sure that the code I used to make it work is the appropriate way, but I will take any win at this point!
I will take absolutely any advice/guidance/google links anyone is willing to share to help me! I'd like to start bootcamp out on the right foot and figure this out.
Thanks in advance!