0
$(function () {
    $max = 4;
    $i = 1;
    for ($i = 1; $i < 3; $i++) {
        $('#itemListLeading table tr :nth-child(' + $i + ')').each(function () {
            $length = $(this).children().text().length;
            if ($length > $max) {
                $max = $length;
            }
            $('#itemListLeading table tr :nth-child(' + $i ' +)').promise().done(function () {
                $(this).css("min-width", $max * 8 + "px");
            });
        });
    };
});

Not sure why this here doesn't work. I'm trying to pass '1' and '2' and maybe more later to the nth-child(). It works on a single nth-child(1), ie. without the for loop. Or is there a better way of passing x amount of variables to run through the function?

Here is a fiddle.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Supernal
  • 3
  • 3
  • http://jsfiddle.net/xNquz/7/ link here as it wouldn't let me post it. – Supernal Apr 29 '13 at 13:41
  • 3
    JavaScript isn't PHP; your variable names don't _need_ start with `$`. – Matt Ball Apr 29 '13 at 13:43
  • You haven't really explained what the problem is. – Felix Kling Apr 29 '13 at 13:44
  • @Matt Ok yeah, I was just testing to see if it would work with that. – Supernal Apr 29 '13 at 13:51
  • @Felix Kling If you go on the js.fiddle it should be a bit clearer I'm trying to pass the number's 1 and 2 to the function so I can make it work for nth-child(1) and nth-child(2), without just copying the function out again and replacing 1 with 2. – Supernal Apr 29 '13 at 13:54
  • No, the demo does not really help me. You said *"Not sure why this here doesn't work."*, but what exactly does not work? What do you expect to happen and what does actually happen? – Felix Kling Apr 29 '13 at 13:59
  • I expect the td child2 and 3 to be sized accordingly '$max * 8' which is why I added in the loop to try to pass those 2 numbers. If you paste this in to replace the script it works for one td row and I want it to work for the next 1. – Supernal Apr 29 '13 at 14:06
  • $(function(){ $max = 4; $('#itemListLeading table tr :nth-child(2)').each(function() { $length = $(this).children().text().length; if ($length > $max){ $max = $length; } $('#itemListLeading table tr :nth-child(2)').promise().done(function() { $(this).css("min-width", $max * 8 + "px"); }); }); }); – Supernal Apr 29 '13 at 14:06

1 Answers1

0

You've got a syntax error in your script:

$(function () {
    $max = 4;
    $i = 1;
    for ($i = 1; $i < 3; $i++) {
        $('#itemListLeading table tr :nth-child(' + $i + ')').each(function () {
            $length = $(this).children().text().length;
            if ($length > $max) {
                $max = $length;
            }
            // Old line
            //$('#itemListLeading table tr :nth-child(' + $i ' +)').promise().done(function () {

            $('#itemListLeading table tr :nth-child(' + $i + ')').promise().done(function () {
                $(this).css("min-width", $max * 8 + "px");
            });
        });
    };
});
Corneliu
  • 2,932
  • 1
  • 19
  • 22
  • Ah yeah thanks. Seems to kind execute atleast now but crashes my browser and asks me to stop the script. Is it just getting stuck in loop? – Supernal Apr 29 '13 at 13:58