Solved

jQuery: Animate parent and cousin with one statement

Posted on 2013-05-29
3
226 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 10

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JQuery conflicts/syntax issue 4 25
[form-control] Retain values after a POST action 21 36
How do I split a variable to newline 2 19
GitHub 1 10
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

733 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