I have multiple fadeouts happening at the same time on various Ids, Is there any way of setting this up in one line as example:
This:
$("#bottle").on('click', function() {
$("#container_inner01").fadeIn(1100);
$("#container_inner02").fadeOut(1100);
$("#container_inner03").fadeOut(1100);
$("#container_inner04").fadeOut(1100);
$("#container02").fadeOut(1100);
})
turned it to this:
$("#bottle").on('click', function() {
$("#container_inner01").fadeIn(1100);
$("#container_inner02,#container_inner02,#container_inner03,#container_inner04,#container02").fadeOut(1100);
})
I am sure its possible and its some syntax error I am doing.
Martin Tournoij
28k24 gold badges110 silver badges158 bronze badges
asked Dec 10, 2014 at 14:19
3 Answers 3
Why not give your elements a class
and execute the script that way?
$("#bottle").on('click', function () {
$("#container_inner01").fadeIn(1100);
$('.test').fadeOut(1100);
});
answered Dec 10, 2014 at 14:26
Comments
Try this
(function($){
$("#bottle").on('click', function() {
$('#container_inner01').fadeIn(1100);
$('#container_inner02,#container_inner03,#container_inner04,#container02').fadeOut(1100);
});
})(jQuery);
answered Dec 10, 2014 at 14:26
Comments
Doing what you have in the second script:
$("#bottle").on('click', function() {
$("#container_inner01").fadeIn(1100);
$("#container_inner02,#container_inner02,#container_inner03,#container_inner04,#container02").fadeOut(1100);
});
Will work, although you can sum this up to this:
$("#bottle").on('click', function() {
$("#container_inner01").fadeIn(1100);
$("#container_inner02,#container_inner03,#container_inner04,#container02").fadeOut(1100);
});
Note that you have #container_inner02,#container_inner02
in you script repeating. Another way to do this is to add a class to all of them and using that class affects each item:
$("#bottle").on('click', function() {
$("#container_inner01").fadeIn(1100);
$(".myClassName").fadeOut(1100);
});
answered Dec 10, 2014 at 14:26
4 Comments
Paul Roub
Including
#container_inner01
in the fadeOut()
instead of fading it in isn't exactly the same as the original two steps.AGE
Your second code ommits the fact that the OP is using fadeIn for #container_inner01 and fadeOut for the remaining containers
Spencer Wieczorek
@PaulRoub Oh your right, didn't notice it was different. I'll edit my answer.
Spencer Wieczorek
@AGE Thanks for pointing that out, I didn't notice the
fadeIn()
and thought they were all fadeOut()
.lang-js
)
at the very end, in both examples. Any errors showing up in the JS console?