Jump to content

[RESOLVED]To insert row in html using JavaScript


Jangrina

Recommended Posts

Hi, I am trying to insert row using for loop. Below is my coding.When I try on browser, nothing displayed.Pls help.

<table border="1">  <tr>	<th>Ref. No.</th>	<th>Track</th>	<th>Title</th>  </tr>  <script language="JavaScript">  for (int 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>

Edited by Jangrina
Link to comment
Share on other sites

FYI, that is not the long term solution, if you are indeed trying to modify any sort of existing markup. document.write is the worst way to update a page, but just to practice with its fine, but only in limited applications.

Link to comment
Share on other sites

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. I am trying this below but still theres no display. Pls look into this and advise me whats need to be done with this code. 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 bodytbody.innerHTML = ""; //empty table bodyfor (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";}}

Edited by Jangrina
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...