I am trying to use the following css code in the table I create in JS but it seems that it doesn’t get any of it’s styles and I don’t know why.
So here’s the code:
CSS Code:
<style type="text/css">
div.team
{
margin-right: 30%;
}
.team table
{
border-collapse: collapse;
}
.team table td
{
background-color: #4F5FAC;
border: 2px groove grey;
}
.team table th
{
font-style: italic;
background-color: #0B1260;
padding: 0 15px;
color: white;
border: 2px groove black;
}
.team tr td:first-child
{
color: yellow;
font-weight: bold;
text-align: center;
}
</style>
JS code:
myWindow=window.open('table.html');
myWindow.document.write("<table class = 'team'>");
myWindow.document.write("<tr><td> שם פרטי: </td><td>" + document.reg.name.value + "</td></tr> <tr><td> שם משפחה: " + document.reg.lname.value + "</td></tr> <tr><td> אימייל: " + document.reg.email.value + "</td></tr> <tr><td> סיסמא: " +document.reg.password.value +"</td></tr>");
myWindow.document.write("</table>");
Any idea why I don’t get the styles of the table?(it works without the js)
Thanks in advance!
I see at least four issues:
table.html.document.write()after the document has been loaded which you are doing here, it then clears out the current document (including all style rules you’ve previously included) and starts a new document. So, there is no way for these style rules to be in effect in thetable.htmlfile because you’ve cleared out the prior contents of that document. If you want to ADD content to the existing page without destroying it’s current contents and styles, then you need to use DOM insertion API calls like.append()or.insertBefore()or set.innerHTMLon an existing DOM object..team tableneeds to betable.teamand.team table thwould need to be:table.team th. When you have a space between identifiers as in.team table, that means you want to match atableobject with an ancestor ofclass="team". If you want an object that is both atableandclass="team", then you have to not have a space between the two identifiers like this:table.team.table.html, you will have to wait for it to load (with theonload()event or one of the otherDOMReadyevents) before its contents are ready to be modified and you cannot usedocument.write().To solve all this, I would suggest this:
This puts style rules in the right file and passes data to the new page while keeping the code for modifying the page within its own page (massively simplifying maintenance going forward).
OK, assuming you don’t need any of the content in table.html, you can create a new window from scratch with this code:
You can see this work here: http://jsfiddle.net/jfriend00/VLt8h/. The style rules won’t apply there because the css file doesn’t have the correct full URL.