Jump to content

Using a drop-down menu to change a stylesheet


Kervtuza

Recommended Posts

Here is a script that should work. I used it on a website once before. This uses a ul instead of form code you could do the same thing with form code though. This is a simple script. You may prefer the link that Ben posted because it saves the option through cookies.

 

HTML

<html>
<head>
<link href="style1.css" id="myCSS">
</head>
<body>
<ul id="navigation">
	<li><a href="#" id="style1">Style 1</a></li>
	<li><a href="#" id="style2">Style 2</a></li>
</ul>
</body>
</html>

 

JS

function style2() {
document.getElementById("myCSS").setAttribute("href", "style2.css");
}
function style1() {
document.getElementById("myCSS").setAttribute("href", "style1.css");
}
window.onload = function() {
document.getElementById('style2').onclick = style2;
document.getElementById('style1').onclick = style1;
}

Link to comment
Share on other sites

  • 1 month later...

Better if you go for Java script instead of HTML,and look at following code:

<script type="text/javascript">

$(function() {

$("#giveAId").change(function(){ //2 step

var stylesheet = $(this).val(); // 3 step

$('link').attr('href',stylesheet+ '.css'); //4 step done here

});

});

 

</script>

 

 

<div style="float:right;padding:26px 0 0 0;color:#fff;">

<select id="giveAId"> // 1 step

<option>please select your choice</option>

<option value="one">blue</option>

<option value="two">pink</option>

</select>

</div>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...