2

I have a WHILE loop that loops through a table as long as $i is less than 10. I want this loop to assign the INNERHTML of each cell to a variable. So that I can use it later on to process the information (the loop is because you can choose if it has to take first 3 or first 10 cells). But how do I assign every innerHTML to a different variable? I'm sure there has to be an easy way. But google couldn't get me a good answer.

Thanks in advance!

Milaan

4
  • What variables do you want to assign them to? Commented Mar 10, 2011 at 15:24
  • lol my answer is the first answer and its the same as most of the others for all intents and purposes and the other guy gets the votes ahaha oh well that's life I guess, good question though Commented Mar 10, 2011 at 15:43
  • @ScottC: It's not a competition. I guess I got the votes because I provided the required Javascript, rather than pseudo-code. :) Commented Mar 10, 2011 at 16:07
  • perhaps! Also it may not be a competition, but its nice to have a correct answer recognized with a vote or two, is it not?? there's nothing saying yours can't be selected as the correct answer while others are still voted up...sorry if I made you feel like it was a competition. Commented Mar 10, 2011 at 17:04

7 Answers 7

2

Arrays are designed for this purpose.

(Presumably you mean i, not $i as Javascript variables do not need to start with $.. though there's no reason you can't, many people choose to use that only for jQuery objects. I will not use them below. Also I've had to guess at the layout of your code.)

var myHTMLs = [];
var i = 0;
while (i < 10) {
   var html = *<no idea how you are getting cell contents here.. what cell?!>*
   myHTMLs[myHTMLs.length] = html;
   i++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this works :) The other answers will probably work too but this answer was on top. Thanks everyone! I know it had to be something like this xD I had almost the same thing but that didn't work. But know it does! Thank you very much! And yes you don't need to use $, but for some reason I think it looks better (atleast, then I can seperate things easier).
@Milaan: No problem. And regarding $, I understand. :)
1

Can you give each cell a unique id? Then you can just do:

var cell_ids = ['id-cell-1', 'id-cell-2',...];
var cellcontents = [];

while (i < 10) {
  cellcontents.push($("#" + cell_ids[i]).html());
  i--;
}

Comments

0

If I'm reading your question correctly, you want to keep the innerHTML of each cell that you loop over, right? In that case, you'd probably want to use an array. Maybe like this:

var i = 0;
var innerHTML_array = [];
while( i < 10 ) {
  innerHTML_array[i] = current_cell.innerHTML;
  i++;
}

Comments

0

This is when arrays are useful:

var i = 0;
var contents = [];
while (i < 10) {
   var div = document.getElementById("foo" + i);
   contents.push(div.innerHTML);
   i++;
}
alert(contents.length);
alert(contents[0]);
alert(contents[1]);

Comments

0

an array?

this is just semi psuedo code but:

var i;
var innerString[];

while (i < 10) {
    innerString[i] = (innerHTML of elementID);
    i++;
}

Comments

0

Maybe this will help:

Try to reference Your table with some id, eg.

<table id="myTable">

then, try this code

var tab = document.getElementById("myTable");
var y = 0, x = 0;

var myContainer = [];

for (y = 0; y < tab.rows.length; y++){
    for (x = 0; x < tab.rows[y].cells[x].length; x++){
        if (!myContainer[y])
            myContainer[y] = [];
        myContainer[y][x] = tab.rows[y].cells[x].innerHTML;
    }
}

Your table content is now holded in myContainer variable. To access cell no 3 in row no 8 You just use:

alert(myContainer[8][3]);

1 Comment

This is some nice coding! Really, i'm going to save this! It's a little bit to advanced to use for my purpose, but I really like it. I'm going to save it for in the future, maybe I can use it. Thanks!
0

You can try using jQuery's .map() to loop over all the cells and return their innerHTML properties as an array.

Example: http://jsfiddle.net/xHH64/1/

var htmlArray = $('tbody td', '#tableID').map(function(i,v){
  return i < 10 ? v.innerHTML : null;
}).get();

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.