PDA

View Full Version : Resolved To create html row using JavaScript



azrina
Mar 3rd, 2013, 04:04 PM
Hi,

I am trying to create rows using for loop. Below is my coding.
When I try on browser, nothing displayed.
What is wrong with the code?
Pls help.


<table border="1">
<tr>
<th>Ref.1 No.</th>
<th>Track</th>
<th>Title</th>
</tr>
<script language="text/JavaScript">
for (var i=1; i<=10; i++)
{
document.write('<tr>')
document.write('<td>' + (100 + i) + '</td>')
document.write('<td> 3 </td>')
document.write('<td><a href="3-Management & Marketing\101.pdf" target="_blank">XXXXXXXXXXXX</a></td>')
document.write('</tr>')
}
</script>
</table>

Philip M
Mar 3rd, 2013, 04:56 PM
<script language="text/JavaScript">

should be

<script type="text/JavaScript">

document.write() is regarded as obsolete. Better to use DOM methods to create the table.

Quizmaster: What car company was originally known as Beyerishe Motoren Werke?
Contestant: Audi.

azrina
Mar 3rd, 2013, 05:33 PM
I read thru internet if i use document.write, it will erase all the contents in the page after it is loaded. I am not so sure about my understanding about this. And, it is advisable to use innerHTML. I think this is what ur trying to explain.

My html code.

<table border="1" >
<tr>
<th>Ref.1 No.</th>
<th>Track</th>
<th>Title</th>
</tr>
<tbody id="CONTENT"><div id="SHOW"></div>
<script type="text/javascript">
createRows('SHOW');
</script>
</tbody>
</table>

My js code.

function createRows()
{
var tbody = document.getElementId("CONTENT"); //tbody = your table body
tbody.innerHTML = ""; //empty table body
for (i=1; i<=10; i++)
{
tr = tbody.insertRow(-1); //append a row in table body
td = tr.insertCell(-1); td.innerHTML = (100+i);
td = tr.insertCell(-1); td.innerHTML = 3;
td= tr.insertCell(-1); td.innerHTML = "Jangan bersedih";
}
}
But still theres no display.
Pls help.

azrina
Mar 3rd, 2013, 06:00 PM
I wrongly type getElementId. It should be getElementByID.
Do JavaScript have compiler/debugger? I used to use compiler like Visual C++ that shows the line no of the syntax error. Do JavaScript have something similar like this?

Philip M
Mar 3rd, 2013, 08:37 PM
I wrongly type getElementId. It should be getElementByID.
Do JavaScript have compiler/debugger? I used to use compiler like Visual C++ that shows the line no of the syntax error. Do JavaScript have something similar like this?

Yes, IE and Chrome (modern browsers) have an error console. Press F12. The Chrome one is marginally better. In Firefox you can use Firebug.