Jangrina Posted February 28, 2013 Share Posted February 28, 2013 (edited) 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 March 4, 2013 by Jangrina Link to comment Share on other sites More sharing options...
justsomeguy Posted February 28, 2013 Share Posted February 28, 2013 Remove "int". Use "var" if you want to define a variable. You should also check your browser's error console, it will show error messages like this. There are links in my signature for the error consoles in various browsers. Link to comment Share on other sites More sharing options...
Jangrina Posted February 28, 2013 Author Share Posted February 28, 2013 I used var but nothing happen. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 28, 2013 Share Posted February 28, 2013 Check your error console, that's the only syntax error I see. Link to comment Share on other sites More sharing options...
thescientist Posted February 28, 2013 Share Posted February 28, 2013 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 More sharing options...
Jangrina Posted March 3, 2013 Author Share Posted March 3, 2013 Okay.Can you tell me what is the best way instead of using document.write? Link to comment Share on other sites More sharing options...
Jangrina Posted March 3, 2013 Author Share Posted March 3, 2013 (edited) 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 March 3, 2013 by Jangrina Link to comment Share on other sites More sharing options...
Jangrina Posted March 3, 2013 Author Share Posted March 3, 2013 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 More sharing options...
justsomeguy Posted March 4, 2013 Share Posted March 4, 2013 Browsers have developer consoles built in, if you're using Firefox then I would recommend the Firebug extension. There are links in my signature to the documentation for each browser's tools. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now