Solved

jQuery: Animate parent and cousin with one statement

Posted on 2013-05-29
3
223 Views
Last Modified: 2013-05-29
Using jQuery, how can I combine these two animate statements into a single animate statement?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {

 $('.info-pops .show-info-pop').click(function () {

  $(this).parent().parent().find('.info-pop').animate({
                height: '+=50'
            }, 333, function () {
                // Animation complete.
  });
  $(this).parent().parent().parent().animate({
                height: '+=50'
            }, 333, function () {
                // Animation complete.
  });

 });

});

</script>
<style type="text/css">

.info-pops {
width: 100px;
height: 200px;
margin: 50 auto;
border: 5px solid black;
position: relative;
}

.info-pop {
width: 20px;
height: 100px;
margin: 50 auto;
border: 5px solid red;
position: absolute;
left: 20px;
}

.show-info-pop {
position: absolute;
right: 0;
cursor: pointer;
text-decoration: underline;
}

</style>

</head>
<body>

<div class="info-pops">
 <div>
  <div class="info-pop"></div>
  <p>
   <a class="show-info-pop">Grow</div>
  </p>
 </div>
</div>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
3 Comments
 
LVL 42

Assisted Solution

by:David S.
David S. earned 50 total points
ID: 39204155
Try this:
$(document).ready(function() {

 $('.info-pops .show-info-pop').click(function () {
  var par = $(this).parents('.info-pops');
  par.add('.info-pop',par).animate({
                height: '+=50'
            }, 333, function () {
                // Animation complete.
  });

 });

});

Open in new window

0
 
LVL 38

Assisted Solution

by:Tom Beck
Tom Beck earned 50 total points
ID: 39204204
You could just go to the parent of all and find the descendants you want to animate:

$(document).ready(function() {
   $('.info-pops .show-info-pop').click(function () {
       $('body').find('.info-pop, .info-pops').animate({
                height: '+=50'
            }, 333, function () {
                // Animation complete.
        });
    });
});
0
 
LVL 9

Accepted Solution

by:
Ishaan Rawat earned 400 total points
ID: 39204219
Try this.,,,...


$(document).ready(function() {

 $('.info-pops .show-info-pop').click(function () {

  ($(this).parent().parent().find('.info-pop'), $(this).parent().parent().parent()).animate({
                height: '+=50'
            }, 333, function () {
                // Animation complete.
  });
  

 });

});

Open in new window

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now