0

i guess this is a pretty simple question.

I want to give my li-tags a width that matches the image thats is inside it. So far i am getting my image-attributes right for each one.

$("#slider1 li img").each(function(){
 var imageWidth = $(this).width();
 var imageHeight = $(this).height();    
 $(this).attr('width', imageWidth);
 $(this).attr('height', imageHeight);
});

But i can't seem to figure out how i transfer the imageWidth to my li-tag. I tried with:

$("#slider1 li").css("width", imageWidth);

But that just renders the same width for all the li's.

Thanks for helping out :)

EDIT: My markup:

<ul id="slider1"> 
<li><img src="tester.jpg"/></li> 
<li><img src="tester2.jpg"/></li> 
<li><img src="tester3.jpg"/></li> 
<li><img src="tester4.jpg"/></li> 
<li><img src="tester.jpg"/></li> 
<li><img src="tester2.jpg"/></li>
</ul> 

4 Answers 4

6

You can directly call just the parent li that contains the img element like this:

$(this).parent('li').css('width',imageWidth);

Note: this only works if the img is only one level deep under the li. Otherwise, use .closest('li') instead. parent will only traverse one level up, where as closest will keep traversing the DOM tree until it finds the element specified.

Sign up to request clarification or add additional context in comments.

2 Comments

You are correct. After reading the documentation, closest() has several benefits over parent() or parents()
This is also setting the same width for all the li's. Hmm it seem like it is taking all the li-parents :)
2

You should do:

 $(this).closest('li').css("width", imageWidth);

like this

$("#slider1 li img").each(function(){
 var imageWidth = $(this).width();
 var imageHeight = $(this).height();    
 $(this).attr('width', imageWidth);
 $(this).attr('height', imageHeight);
 $(this).closest('li').css("width", imageWidth);
});

7 Comments

This is the shizzle ! Thank you! testing :)
Humm wierd thing. The $(this).closest('li').css("width", imageWidth); is setting the same width for all the li's - again.
show us your markup. this code assumes you have an img inside a 'li' or maybe create a fiddle
I made this fiddle: if you look at the result with firebug you can see that the result is correct jsfiddle.net/kV9Lr (first li: width 200px, second li 300px, fourth li 400px)
It does not set the style="width:xx" for the li's. And my image(s) has not got a width-attr before i run my function.
|
0
$("#slider1 li img").each(function(){
    var imageWidth = $(this).width();
    var imageHeight = $(this).height();    
    $(this).parent().attr('width', imageWidth);
    $(this).parent().attr('height', imageHeight);
});

Comments

0

That's because the selector you're using for setting your '#slider1 li' width is setting them all at the same time (to whatever the last imageWidth was). You need to only set the .parent() width.

Comments

Your Answer

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.