pauldboughter Posted February 24, 2010 Report Share Posted February 24, 2010 Another question... I am triyng to write HTML code inside this script tag. I know that this code isn't write cause ti won't work. Just trying to figure out what I am doign wrong.<div id="data_list"> <script type="text/javascript"> document.write("<table border='1' rules='rows' cellspacing='0'>"); document.write("<tr>"); document.write("<th>Date</th><th>Amount</th><th>First Name</th>"); document.write("<th>Last Name</th><th>Address</th>"); document.write("</tr>"); for (var i = 0; < amount.length; i++) { commands involving amount; if (i % 2 == 0) { document.write("<tr>"); else document.write("<tr class='yellowrow'>"); } document.write("<td>date</td><td class='amt'>amount</td>"); document.write("<td>firstName</td><td>lastName</td>"); document.write("<td>street<br />city, state zip</td>"); document.write("</tr>"); } </script> Link to comment Share on other sites More sharing options...
Synook Posted February 24, 2010 Report Share Posted February 24, 2010 Not writing anything at all? Do you have a link to your page? Note that you can't interpolate variables into strings in JS - you have to concatenate. Link to comment Share on other sites More sharing options...
jeffman Posted February 24, 2010 Report Share Posted February 24, 2010 (edited) for (var i = 0; < amount.length; i++) {Missing i in the conditionalcommands involving amount; Is that supposed to be a comment? Uncommented text will terminate the script before it even runsI saw the second one right off. Both get caught by my Firefox Error Console. It's a good tool, easy to use. Edited February 24, 2010 by Deirdre's Dad Link to comment Share on other sites More sharing options...
nonails Posted February 25, 2010 Report Share Posted February 25, 2010 (edited) for (var i = 0; i < amount.length; i++) { commands involving amount...; if (i % 2 == 0) { document.write("<tr>"); else document.write("<tr class='yellowrow'>"); } document.write("<td>date</td><td class='amt'>amount</td>"); document.write("<td>firstName</td><td>lastName</td>"); document.write("<td>street<br />city, state zip</td>"); document.write("</tr>"); }Big Papa,At the time you posted this, you'd added the dreaded commands involving amount line in another place - one where it wasn't yesterday. Similarly, as Deirdre's Dad pointed out, the i is missing from this loop, as well. Don't confuse this loop (for displaying data and making every second table row yellow) with the other one (for cycling through the array named amount, and adding up its values).Other than that, you really need to watch your curly brackets (they're called braces). One reason why the above script will not run how your homework exercise says is that your bracket nesting is wrong:The for loop has absorbed every line of code there. It needs to close after the total variable is updated (before the if conditional). else cannot be "inside" the if statement it complements. else is a conditional, just like if - both of them need to open and close. One does not "belong" to the other. Also, you need to split code for HTML to recognise from keywords that only Javascript understands. What's the difference between my line and your line? document.write("<td>firstName</td><td>lastName</td>"); // your linedocument.write("<td>" + firstName + "</td><td>" + lastName + "</td>"); // my line There's a start.Regards,No Nails Edited February 25, 2010 by No Nails Link to comment Share on other sites More sharing options...
pauldboughter Posted March 1, 2010 Author Report Share Posted March 1, 2010 Would that not make the + firstName + a variable instead of a header?document.write("<td>firstName</td><td>lastName</td>"); // your linedocument.write("<td>" + firstName + "</td><td>" + lastName + "</td>"); // my line Big Papa,At the time you posted this, you'd added the dreaded commands involving amount line in another place - one where it wasn't yesterday. Similarly, as Deirdre's Dad pointed out, the i is missing from this loop, as well. Don't confuse this loop (for displaying data and making every second table row yellow) with the other one (for cycling through the array named amount, and adding up its values).Other than that, you really need to watch your curly brackets (they're called braces). One reason why the above script will not run how your homework exercise says is that your bracket nesting is wrong:The for loop has absorbed every line of code there. It needs to close after the total variable is updated (before the if conditional). else cannot be "inside" the if statement it complements. else is a conditional, just like if - both of them need to open and close. One does not "belong" to the other. Also, you need to split code for HTML to recognise from keywords that only Javascript understands. What's the difference between my line and your line? document.write("<td>firstName</td><td>lastName</td>"); // your linedocument.write("<td>" + firstName + "</td><td>" + lastName + "</td>"); // my line There's a start.Regards,No Nails Link to comment Share on other sites More sharing options...
thescientist Posted March 2, 2010 Report Share Posted March 2, 2010 (edited) no, its telling the browser to put the value of firstName there. In that case, + is being used to create a string (sort of like "adding" words together to make a sentence) but in this, case firstName and lastName can be dynamic. In either case it wouldn't be a header. It could be a heading if you were wrapping it in <h1-6> tags instead of <td> tags. Edited March 2, 2010 by thescientist Link to comment Share on other sites More sharing options...
justsomeguy Posted March 2, 2010 Report Share Posted March 2, 2010 Maybe this will make it more clear. Using this line:document.write("<td>" + firstName + "</td><td>" + lastName + "</td>");will have the same effect as this: var start = '<td>';var middle = '</td><td>';var end = '</td>';document.write(start + firstName + middle + lastName + end); Those two pieces of code will output the same text to the browser. Also, you can use a <th> element to define a table heading. 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