Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-29-2011, 12:21 PM   PM User | #1
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 806
Thanks: 92
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Access the LI and link is in

I have a list and each li in it has a link in, and i want to animate the li the link is in when its clicked. How wouldi do this? Would it be parent i need to look at?
tomharto is offline   Reply With Quote
Old 10-29-2011, 12:37 PM   PM User | #2
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,827
Thanks: 9
Thanked 685 Times in 679 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Yes, use .parent().

If your html is something like:

Code:
<ul>
<li><a href="#">Link One</a></li>
<li><a href="#">Link Two</a></li>
<li><a href="#">Link Three</a></li>
</ul>
then

Code:
$('li a').click(function(){
	$(this).parent().animate({'marginLeft':'30px'})
	return false;
});
will animate the parent li.

However, if the li animates when the link is clicked then I'm assuming the link does not go to another URL - in which case you might not need the anchor tags - and then you can just attach a click event to the li itself. Just a thought...
SB65 is offline   Reply With Quote
Old 10-29-2011, 12:40 PM   PM User | #3
tomharto
Regular Coder

 
Join Date: Jul 2010
Location: Sheffield
Posts: 806
Thanks: 92
Thanked 18 Times in 18 Posts
tomharto is on a distinguished road
Well when the link is clicked im gonna use the href to run AJAX query, i just needed to sort the height thing.

That you posted works great thanks . How would i go about toggling it so when you click the link a second time it goes back to the normal height (40px)?
tomharto is offline   Reply With Quote
Old 10-29-2011, 01:12 PM   PM User | #4
SB65
Senior Coder

 
Join Date: Feb 2009
Location: West Yorkshire
Posts: 2,827
Thanks: 9
Thanked 685 Times in 679 Posts
SB65 will become famous soon enoughSB65 will become famous soon enough
Not sure what you're doing with the height, but using the margin example:

Code:
$('li a').click(function(){
	if ($(this).hasClass('active')) $(this).parent().animate({'marginLeft':'0'});//element is already active, so revert to 'normal'
	else $(this).parent().animate({'marginLeft':'30px'});//element is not active so animate it
	$(this).toggleClass('active');//toggle the class
	return false;
});
A few ways of doing this, but this'll work.
SB65 is offline   Reply With Quote
Old 10-29-2011, 01:13 PM   PM User | #5
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,697
Thanks: 5
Thanked 875 Times in 850 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
Do a conditional check on click and animate appropriately:
Code:
$('li a').click(function(e){
  if($(this).parent().height() > 40) {
	$(this).parent().animate({'height':'40px'})
  }
  else {
	$(this).parent().animate({'height':'80px'})
  }
	e.preventDefault();
});
Edit: SB65’s way of using classes is even better since you can style the elements more flexibly.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:26 PM.


Advertisement
Log in to turn off these ads.