-2

I make the following code after hard research and also make it work, i done google research, but can't get how to combine these if statements, i know that they can be written easier way but how in my case, also please tell me how to increase 500px each time, is it possible in my case, please help...

for (var i = 0, row; row = document.getElementsByTagName('table')[0].rows[i]; i++) {
    if (row.offsetHeight > 200) {
        document.getElementsByTagName('table')[0].style.width = "1500px";
        if (row.offsetHeight > 200) {
            document.getElementsByTagName('table')[0].style.width = "2000px";
            if (row.offsetHeight > 200) {
                document.getElementsByTagName('table')[0].style.width = "2500px";
            }
        }
    }
}
xkickflip
  • 768
  • 6
  • 17
Mr. Rick
  • 155
  • 1
  • 14
  • Please format your questions clearly. Also have any errors occurred, if so did you check the console? Can you post them on here if they did. – Script47 Aug 09 '15 at 02:09

2 Answers2

2

Looks like you could make use of while

var i = 0, j, row, table = document.getElementsByTagName('table')[0];
j = table.offsetWidth;
while (row = table.rows[i++]) {
    while (row.offsetHeight > 200 && j < 2500) {
        j += 500;
        table.style.width = j + 'px';
    }
}
Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • thanks for your answer, I tried `j = $('table').width()` but not working, can you please tell me how to make it work to get my table width instead of 1000px... – Mr. Rick Aug 12 '15 at 04:27
  • @Mr.Rick You could move the setting of _j_ outside the outer loop and set it to the `offsetWidth` of the table, see edit – Paul S. Aug 12 '15 at 17:24
  • thanks i also removed extra var j without any value, `var i = 0, row, table = document.getElementsByTagName('table')[0], j = table.offsetWidth; while (row = table.rows[i++]) { while (row.offsetHeight > 160 && j < 4000) { j += 300; table.style.width = j + 'px'; } }` Is my code okay, thanks for your answer and time. – Mr. Rick Aug 12 '15 at 20:46
  • 1
    @PaulS. You know whats funny? Mr. Rick asked this question to answer this question from Jan. 2012. http://stackoverflow.com/questions/8970362/setting-a-max-height-on-a-table/31932474#31932474 look familiar? lol – Adam Buchanan Smith Aug 12 '15 at 23:09
  • 1
    @AdamBuchananSmith hopefully he learnt something. You know something even more funny though, follow the [**"source reference"**](http://stackoverflow.com/questions/31899073/html-table-solution-max-height-limit-for-rows-or-cells-by-increasing-table-width/) to a third question which is both asked and answered by him and _`"i found the solution myself"`_ and no references here – Paul S. Aug 13 '15 at 02:13
0

if you want to call all the document.getElement... from a single if statement, the format you need is:

if (row.offsetHeight > 200)
{
    document.getElements...1;
    document.getElements...2;
    //and so on.
}

The curly brackets are how you tell the browser that all the sub statements should only be executed if the condition returns true.

for (var i = 0, row = document.getElementsByTagName('table')[0].rows[i]; i < row; i++) {
    if (row.offsetHeight > 200) 
    {
        document.getElementsByTagName('table')[0].style.width = "1500px";
        document.getElementsByTagName('table')[0].style.width = "2000px";
        document.getElementsByTagName('table')[0].style.width = "2500px";
    }
}

Honestly, I'm not sure what effect you are after though. What this is going to do is test for row.offsetHeight > 200, and in three steps increment ('table')[0]'s style up to 2500px, and then reiterate that process row times. If you can explain what effect you are trying for, I might be able to help more.

var table = document.getElementsByTagName("table")

while (row.offsetHeight > 200)
{
table[0].style.width += 500;
}

I think something like this might generate the behavior you're looking for.

Chris
  • 126
  • 1
  • 11
  • Then will the performance be increased, can you write it complete ? – Mr. Rick Aug 09 '15 at 13:55
  • @Mr.Rick, I've updated my answer a bit. If you can explain a little more what you are trying to achieve I might be able to be more help though. – Chris Aug 10 '15 at 03:04
  • i want to increase the table width until each row height shrink to 200px. – Mr. Rick Aug 10 '15 at 03:09
  • i think , in your code it will not check row height again and increase direct width to 2500px, this may result in excess width without necessicity – Mr. Rick Aug 10 '15 at 03:11
  • so what you need is something along the lines of a while statement which loops through and increases row length and tests height on each iteration. – Chris Aug 10 '15 at 03:57
  • yes my code and accepted answer both working , i just wanted the optimal code, thanks for your comments and time... – Mr. Rick Aug 10 '15 at 06:06