Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Changing CSS style with JavaScript
Old 10-22-2008, 09:16 AM [resolved] Changing CSS style with JavaScript
Junior Talker

Posts: 3
Name: reQuorter
Trades: 0
Hello guys!

I want to write script in JavaScript that will change CSS style with radio buttons. Here is my code:

HTML Code:
<input type="radio" name="style" onclick="changeCSS(1);" />Style 1<br />
<input type="radio" name="style" onclick="changeCSS(2);" />Style 2<br />
Here is my JavaScript code:

Code:
function changeCSS(stylCSS)
{
this.stylCSS = stylCSS;

if (stylCSS == "1") { document.write('<link rel="stylesheet" href="style.css" type="text/css">'); }
if (stylCSS == "2") { document.write('<link rel="stylesheet" href="style2.css" type="text/css">'); }
}
This isn't working... when I click button my page starts to loading forever...
Please help, I'm JS beginner.

Last edited by reQuorter; 10-22-2008 at 09:58 AM.. Reason: resolved
reQuorter is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-22-2008, 09:43 AM Re: Changing CSS style with JavaScript
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
This isn't working...
And it never will!

writing the stylesheet reference in like that will only work at page load.

You will have to save the value to a cookie and reload the page.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-22-2008, 09:57 AM Re: Changing CSS style with JavaScript
Junior Talker

Posts: 3
Name: reQuorter
Trades: 0
Problem resolved:

HTML Code:
<head>
<link id="stylesheet" rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript">
function changeCSS(stylCSS)
{
var ss=document.getElementById('stylesheet')

if (stylCSS == "1")
 ss.href='style.css';
else
 ss.href='style2.css';
}
</script>
</head>
<body>
<input type="radio" name="style" onclick="changeCSS(1);" />Style 1<br />
<input type="radio" name="style" onclick="changeCSS(2);" />Style 2<br />
</body>
reQuorter is offline
Reply With Quote
View Public Profile
 
Old 10-22-2008, 10:05 AM Re: Changing CSS style with JavaScript
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,985
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
That's an unusual approach. Did you test it in all the browsers?
You can import stylesheets like this:
Code:
var headID = document.getElementsByTagName("head")[0];         
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = 'example.css';
cssNode.media = 'screen';
headID.appendChild(cssNode);
As far as changing the style of just one element on the page, you can do it with that element's className or style property.
__________________
Join me on
Please login or register to view this content. Registration is FREE
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Reply     « Reply to Changing CSS style with JavaScript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19092 seconds with 12 queries