-1

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
9
  • What you have should work. Commented Dec 10, 2014 at 14:21
  • What's your problem? Do you get an error? Do only a number of animations work? Does nothing happen at all? Commented Dec 10, 2014 at 14:22
  • You're missing a ) at the very end, in both examples. Any errors showing up in the JS console? Commented Dec 10, 2014 at 14:22
  • As a side note, you've got #container_inner02 in there twice on your new line Commented Dec 10, 2014 at 14:25
  • @PaulRoub - no he's not. Commented Dec 10, 2014 at 14:30

3 Answers 3

2

Why not give your elements a class and execute the script that way?

$("#bottle").on('click', function () {
 $("#container_inner01").fadeIn(1100);
 $('.test').fadeOut(1100);
});

JsFiddle demo

answered Dec 10, 2014 at 14:26

Comments

0

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

0

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

Including #container_inner01 in the fadeOut() instead of fading it in isn't exactly the same as the original two steps.
Your second code ommits the fact that the OP is using fadeIn for #container_inner01 and fadeOut for the remaining containers
@PaulRoub Oh your right, didn't notice it was different. I'll edit my answer.
@AGE Thanks for pointing that out, I didn't notice the fadeIn() and thought they were all fadeOut().

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.