/    Sign up×
Community /Pin to ProfileBookmark

CSS Styles not working with Javascript onstart function

I am new to the forum, this should be a simple question though. First off thanks for any help you guys can provide. When I run this code the css doesn’t show up, but the code runs properly. I have cut out everything but the meat. Let me know what I am doing wrong. This is for my JavaScript class so explanations are very valuable to me!

[code]
<style type = “text/css”>
table { width: 10em }
th { text-align: left }
</style>

<script type = “text/javascript”>
<!–
function Arrays()
{
var color = new Array( “cyan”, “magenta”, “red”, “black” );
var even = new Array( “2”, “4”, “6”, “8” );
var integer = [0 , , , 6];

for ( var i = color; i < color.length; i++ )
color[ i ] = i;

for ( i = even; i < even.length; i++ )
even[ i ] = i;

for ( i = integer; i < integer.length; i++ )
integer[ i ] = i;

outputArray( “Array colors contain”, color );
outputArray( “Array integers contain”, even );
outputArray( “Array integers1 contain”, integer );
}

function outputArray( header, whichArray )
{
document.writeln( “<h4>” + header + “</h4>” );
document.writeln( “<table border = “1”” );
document.writeln( “<thead><th>Subscript</th>” +
“<th>Value</th></thead><tbody>” );

for ( var i = 0; i < whichArray.length; i++ )
document.writeln( “<tr><td>” + i + “</td><td>” +
whichArray[ i ] + “</td></tr>” );

document.writeln( “</tbody></table>” );
}
// –>
</script>
</head><body onload = “Arrays()”></body>

[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@opifexSep 22.2009 — seems that you left out the "tr" element; "th" resides inside a "tr"...
Copy linkTweet thisAlerts:
@TheFamilyGeekauthorSep 22.2009 — I added that in like this:
<i>
</i> document.writeln( "&lt;tr&gt; &lt;thead&gt; &lt;th&gt;Subscript&lt;/th&gt;" + "&lt;th&gt;Value&lt;/th&gt; &lt;/thead&gt; &lt;/tr&gt; &lt;tbody&gt;");


and the only difference that I could tell was that the cells changed spacing slightly. I changed the alignment and the size of the table width to test it and there was no change. Did I write the above line correctly this time?
Copy linkTweet thisAlerts:
@mrhooSep 22.2009 — none of your for loops in the first function will ever run, because no array is less than its length.

calling document.write opens a new page, it will only contain what you write- and you should call document.close() when you finish writing.
Copy linkTweet thisAlerts:
@TheFamilyGeekauthorSep 22.2009 — The loop runs because the new variable i is less than the length of the array. The only problem is that the CSS styles are not being used when you run it. From the literature that I can find online the only time that you need to use document.close() is when you use document.open() to open the document stream. If there is more to it than that please explain it to me because we haven't covered document.close() or document.open() in class yet.
×

Success!

Help @TheFamilyGeek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...