I have a 'div' that I 'fadeOut' after pressing a button using javascript. After the 'div' has faded out I would like it to 'fadeIn'. This is presently not happening for me. I understand the concept of the callback function but I appear not to have succeeded in implementing it in my code. Please can someone advise? I would like a callback function solution if that is possible.
function fade_effect(element, callback) {
var x = function(element) {
var elem = document.getElementById(element);
elem.style.transition = "opacity 2.0s linear 0s";
elem.style.opacity = 0;
return elem;
}
var thisElement = x(element);
fadeIn(thisElement);
}
function fadeIn(element_input) {
element_input.style.transition = "opacity 2.0s linear 0s";
element_input.style.opacity = 1;
}
div#box1 {
background: #9dceff;
width: 400px;
height: 200px;
}
<button onclick="fade_effect('box1',fadeIn);">Magenta</button>
<div id="box1">Content in box 1 ...</div>
fadeInfunction is accepting aelement_inputattribute and you're trying to useeleminside the function?x()takes one argument, why are you calling it with two arguments?fade_inis using a variableelemthat hasn't been defined. There's a variable with this name inx, but its scope is local to that function.