Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble using CSS inside JavaScript

Status
Not open for further replies.

Dragonfish

Programmer
Apr 25, 2001
62
UG
When the JavaScript-funtion calls "Update" then the text - This is a Test - is writen but not formatted with class='style_Mannschaften'.

function Update()
{
document.write(&quot;<p class='style_Mannschaften'>This is a Test</p>&quot;);
}

The CSS line looks like this:
.style_Mannschaften { border: none; position: absolute; top: 100px; left: 570px; width: 250pt; }

If I simply write This is a Test in the HTML document using the style sheet then it works

So my problem is how to integrate CSS into Javascript. Other HTML tags - as we all Know - work in JavaScript. Later when this works I´d like to know how to integrate PHP-statements into JavaScript to access a MySQL Database but I have´nt found any interdeciplinary literature in WWW. Can anybody help

Thanks - David in Germany
 
It is very likely that the Update function is rewriting the whole page. Because of this it doesn't know what .style_Mannschaften is because it is not defined.

If you change your Update function to the following I think it will do what you want:

function Update()
{
document.write(&quot;<p style='border: none; position: absolute; top: 100px; left: 570px; width: 250pt;'>This is a Test</p>&quot;);
}
 
Hi David.. you could also use DHTML if you're not to worried about browser compatibility. ie:

Code:
<html>
<head>
	<title>Untitled</title>
	<script>
	function changeIt(){	
	block.innerText = &quot;new text&quot;;
	}
	</script>
</head>

<body>
<a href=&quot;javascript:changeIt()&quot;> change it</a><br><br>
<span id=&quot;block&quot; class=&quot;style_Mannschaften&quot;>old text</span>

</body>
</html>

As for combining the two (php/js), you just have to remember that JS is completely run on the client and the PHP is completely on the server... once you get used to that, it's easy, you use JS to assign form values on the page, then when it is submitted/reloaded, you can get that data to PHP from the querystring, that's how you transfer data from JS-->PHP. To get it back into javascript, you can either write the JS with PHP, or assign form values via the PHP that javascript can read again once the page has loaded. There aren't any books that I know of specifically on integrating the two, but since they only interact in the manner I describe above, if you have good PHP and JS books, you should be fine.

-Scott
 
Thanks aperfectcircle, thanks Scott,

I changed the JS as you recommended:
document.write(&quot;<p style='border: none; position: absolute; top: 100px; left: 570px; width: 250pt;'>This is a Test</p>&quot;);

Again the Page was completely rewriten, but this time formatted correctly. Pity I´d hoped I could use an external .css file (frustrated purist).

Be grateful for any more ideas on how to stop JS rewriting the whole page

David in Germany
 
You can use innerHTML to keep it from writing the whole page...
Or you could just have it write out the <html>,<head>,<link>, etc. tags with the <p> tag. That would allow you to still use an external stylesheet...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top