LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2014, 10:43 AM   #1
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, XUbuntu 12.04
Posts: 483

Rep: Reputation: 37
Javascript: insertRow()


I'm just experimenting with creating HTML tables in Javascript using the DOM, i.e. createElement, insertRow and insertCell. Am I right to think that, upon creating a new row in a table, you then have to separately create the cells within that row, using insertCell() within a For loop?

I mean, for a two row table, each row containing 13 cells, is there a more efficient method than doing:

Code:
var table = document.createElement('table');
for(i=0; i<2; i++){
   var row = table.insertRow(0);
   for(j=0; j<13; j++){row.insertCell(j);}
}
document.getElementById("something").appendChild(table)
Two nested For loops. Is there no quicker way?

Is there no insertRow() type of method that just says "insert a new row with the same number of cells as the one above"?

Last edited by Steve W; 02-26-2014 at 11:15 AM.
 
Old 02-26-2014, 12:10 PM   #2
dugan
Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 5,626

Rep: Reputation: 1766Reputation: 1766Reputation: 1766Reputation: 1766Reputation: 1766Reputation: 1766Reputation: 1766Reputation: 1766Reputation: 1766Reputation: 1766Reputation: 1766
That's just 26 iterations, but you could just modify the innerHTML property.
 
1 members found this post helpful.
Old 02-28-2014, 01:06 PM   #3
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, XUbuntu 12.04
Posts: 483

Original Poster
Rep: Reputation: 37
Yes, that's how I was doing it before. But I read somewhere that innerHTML with table tags was an unreliable method that did not work properly in Internet Explorer. I can't test this, because I don't have it installed.

What I was doing was this:

Code:
gridBody+='<table border="0" cellpadding="0" cellspacing="0">';
for(var r=0; r<2; r++)
{
	gridBody+='<tr>';
	for(var c=0; c<13; c++)
	{
		gridBody+='<td></td>'
	}
	gridBody+='</tr>'
}
gridBody+='</table>'
document.getElementById("something").innerHTML = gridBody;
Same kind of loop structure, just adding the tags to a variable, then placing the variable contents using innerHTML.

So basically these are the two methods, and that's about as straightforward as it gets, short of using JQuery?
 
Old 03-01-2014, 04:44 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,172

Rep: Reputation: 259Reputation: 259Reputation: 259
Hi

The innerHTML method is usually faster, and does work with Internet Explorer. It has problems if the table is not complete, but you can write complete tables or contents of individual cells with innerHTML.

When it's only 26 there's probably not much difference. For every change in the DOM, the browser needs to reflow the entire document. That's why it's better to write the table in one operation.
 
1 members found this post helpful.
Old 03-01-2014, 12:32 PM   #5
Steve W
Member
 
Registered: Mar 2007
Distribution: Ubuntu 12.04, XUbuntu 12.04
Posts: 483

Original Poster
Rep: Reputation: 37
Okay, thank you for the information.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript paul62 Linspire/Freespire 3 11-29-2009 07:02 PM
Looking for Textbrowser with JavaScript Support or Elink Javascript Support rohezal Linux - Software 4 09-01-2009 01:02 PM
JavaScript dhanhar Linux - Newbie 8 09-28-2007 02:03 AM
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
Javascript help darin3200 General 4 09-03-2005 11:58 PM


All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration