Hi,
Do you want to randomize the rows and then the columns within the rows, or randomize everything (so that column 1 in row 3 becomes column 5 in row 1)?
Main Topics
Browse All TopicsDear EE
I have HTML table and the table contains 5 Rows and 5 columns. I want every time this HTML page is called by the user to view the cells (rows and columns) randomly. How can I do that from JavaScript? The code offered in the solution of ID: 22658210 works great to shuffle the rows each time the page is loaded but I can't figure out how to apply it to the columns as well.
THANKS
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Wow, this is certainly an interesting question.
First, we need to figure out how to "exchange" cell contents.
This is easy to say, and a bit challenging to do. Why? Well,
part of the reason is that cells contents (i.e., what is between
the <td> and </td>) may be non-trivial. For example, we might
have a nested table.
Additionally, if we take a look at the DOM for something simple
like:
<td id='target'>
<table border='1'>
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>C</td>
<td>D</td>
</tr>
</table>
</td>
each browser may "render" this differently. Looking at what FireFox
generates, we see that within the TD node, we have 3 children:
[ 0 ] is a text node with " "
[ 1 ] is the TABLE node
[ 2 ] is a text node with " "
Why it does it this way is not immediately obvious.
So, is there a way to "swap" two cells?
See the attached code for one way to do it.
Now, I'm going to have to think about a complete randomization of
the table contents...
Sorry for the long delay due to health issues. I had no idea that this shuffle of cells in a table would be so complex. Perhaps seeing the actual web page will be useful. The page I need the code for is http://www.vagliardoinspec
Sorry to hear that you were under the weather. I hope that it wasn't anything
serious, and that you are completely recovered.
One of the errors in the code on your test_swap page is that the random
numbers that are generated are not checked for duplicates. So, it is possible
for multiple table cells to have the same value. I'm quite certain that that
particular realtor wouldn't mind in the least. However, any whose image
weren't selected might be less than happy about it... :-)
One thing that might make the code simpler, and easier to understand would be
to have your table cells "numbered" with unique id attributes. As shown in the
"code snippet" section (this portion uses a proportional font, so I had to put
the example there so it would look acceptable):
Then, your code could randomly assign positions (with no duplicates allowed),
and display the table.
Does this make sense? If you want me to proceed with this, let me know.
Q: What should be done if you don't have "enough" images to fill the table?
Q: What if you have too many images to fit in the table?
Q: Would you prefer the whole table to be generated based upon the number
of images available?
Now this is really getting complicated. You raise some great questions. Looks like I didn't give the problem nearly enough thought. If there are not enough images to fill the table I will stuff the empty cells with a "your name could be here" graphic (or something similar). I suppose if I have too many images for the table I would have to manually add a new row and fill any empty cells again with a place holder graphic. The page http://www.vagliardoinspec
Images are only occasionally added at the request of the site owner when he establishes a relationship with new realtor companies. He only added 2 or 3 last year. I generally copy the realtors logo from their website and add descriptive text and an external link to open the realtor's website in a new window. All of the realtors logos are kept in \images but could be seperated from all other website images and graphics by moving them to a new folder like \realtor_images. Hope this answers the question.
Business Accounts
Answer for Membership
by: JohnSixkillerPosted on 2008-04-14 at 00:12:11ID: 21348093
Hi,
I am afraid that there is no simple way to swap columns. Propably only way to do that is to cycle trough TABLE rows and swap particular cells. So randomize first row and apply the same pattern for the rest of the TABLE.