views:

59

answers:

3

Hi, I have an unordered list of links.

<ul>
<li><a href="#">Link is really really really really LONNNNNNNG</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>

I have set the line-height or the list items to 30px.

ul {width:100px;}
ul li {line-height:30px;}
ul li a {display:block;}

I want to change this on any <li> where the texts wraps to a second line, without affecting the other <li>s. What is the best way to do this in jquery? Thanks in advance.

A: 

You should use padding: or margin: instead and omit your line-height. I assume that you want to keep a consistent amount of spacing between each li; use margin and padding instead.

dave
i tried that, but i was getting some funny results on other browsers. I feel like it would be better to add js to dynamically change the line-height rather than try to "eyeball" it. there are also hover effects that won't work if i add a margin
Gerald
I am not really sure what all you are trying to accomplish, but IMHO you should hypothetically be able to accomplish all of this with CSS and using jquery for style (outside of the context of animation/hover effects) is not the best practice
dave
A: 

You use plain Javascript to test the string length of each li element - see this question - and modify it how you want (with Javascript, JQuery or another JS Library).

Dave Everitt
A: 

Thanks for the suggestion, Dave. I think i figured it out. this is what I did:

$('ul li').each(function(){
var menuHeight = $(this).outerHeight();                               
if(menuHeight != 30){
$(this).addClass('squeeze');
}else{
$(this).removeClass('squeeze'); }
});

basically i'm looking through each<li> and saving the height in a variable. if the <li> is not 30px, addClass 'squeeze'. Prob don't need the else statement, but i threw it in for shits and giggles.

thanks everyone

Gerald