1

I am looping through a set of images, and then setting their height to match a variable. The following code is working just fine with Firefox, however is unresponsive in Android's browser. Thoughts?

var x = 100;
count_li = 5;

for (var i = count_li - 1; i >= 0; i -= 1) {
    var carousel_img = '#carousel_img_' + [i];
    $(carousel_img).css('height', x);
}

Where I have a set of 5 images that might look like this:

<img id="carousel_img_0" src="img_0.jpg"/>
<img id="carousel_img_1" src="img_1.jpg"/>
etc...

Here is a similar post for reference, but I'm not finding what I need in it.

A secondary, directly connected issue is that this code is having the exact issue:

var img_w = $(carousel_img).width();

Presumably it too needs a 'px' reference, but I'm unaware of the syntax.

Solutions ----------------------------------------------------------

As stated by Matt, this

$(carousel_img).css('height', x);

needed to have a 'px' added to the end, as seen below:

$(carousel_img).css('height', x + 'px');
Community
  • 1
  • 1
cranberry
  • 294
  • 5
  • 17
  • 3
    As a *guess* try adding a unit to the height property (`var x = '100px';`). – Matt Mar 18 '13 at 18:07
  • Still shooting me '0' every time. – cranberry Mar 18 '13 at 18:11
  • and if you affect height after the load ? $(carousel_img).on("load",function(){ $(this).height(x); }); – r043v Mar 18 '13 at 18:14
  • Try `alert($('[id="' + carousel_img.slice(1) + '"]').length);` inside your `for` loop and check you get `1` each time. This will check that you haven't got duplicate ID's on the page, and that your `css()` call is actually targeting an element (although I've got no idea why it won't be). Can you also show the code you're using that's showing `0`? – Matt Mar 18 '13 at 18:14
  • 2
    ... also, note you don't need the `[` and `]` surrounding `[i]` within your loop (`i` will suffice just fine). Curiously, what `[i]` does is create an array which contains only one element (`i`), and then the fact you're `+`'ing it with a string calls `toString()` on the array, which gives you a comma separated list of calling `toString()` on each element, but because you've only got one element, you're getting `i` back (quite a lot of work when you can just use `i`, I think you'll agree ;)).. – Matt Mar 18 '13 at 18:16

0 Answers0