Forum Moderators: open

Message Too Old, No Replies

Simple JS Help Change class via select / option

Change class via select

         

vlad_i

9:10 am on Oct 7, 2010 (gmt 0)

10+ Year Member



Hello Guys,

I am new at JS can anyone tell me why my code isn't working. I am sure its something really stupid :(

I am trying to change class depending on the option selected. I can get it to say hello world, but not change class. Where am I off?

<html>
<head>
<style>
#chosen{height:300px; width:600px; background-color:#CCCCCC}
.class1{background-color:#000000; color:#fff}
</style>
<script language="javascript">
<!--
function changeClass(chosen)
{
theBox = document.getElementById(chosen);

if (chosen == " ") {
theBox.className = "class1";
}
if (chosen == "1") {
alert("you chose1");
}
if (chosen == "2") {
alert("22222222!");
}
if (chosen == "3") {
alert("33333!");
}
}
-->
</script>
</head>
<body>
<div align="center">
<form name="myform">
<select name="optone" size="1"
onchange="changeClass(document.myform.optone.options[document.myform.optone.selectedIndex].value);">
<option value=" " selected="selected"> </option>
<option value="1">First Choice</option>
<option value="2">Second Choice</option>
<option value="3">Third Choice</option>
</select>
</form>


<div id="chosen">
This needs to change class
</div>
</div>
</body>
</html>

birdbrain

10:39 am on Oct 7, 2010 (gmt 0)



Hi there vlad_i,

and a warm welcome to these forums. ;)

Actually your code did change the className. :)

The problem was with your CSS.
Values that are set by id cannot be overridden by those of class

I have also taken the liberty of updating/simplifying your code...


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">

<title></title>

<style type="text/css">
#myform {
text-align:center;
}
#chosen{
height:300px;
line-height:300px;
width:600px;
margin:auto;
text-align:center;
}
.class0 {
background-color:#ccc;
color:#000;
}
.class1 {
background-color:#000;
font-size:1em;
color:#fff;
}
.class2 {
background-color:#f00;
font-size:1.5em;
color:#00f;
}
.class3 {
background-color:#00f;
font-size:2em;
color:#f00;
}
</style>

<script type="text/javascript"> /* Note that language="javascript" is now deprecated */

function changeClass(){

document.getElementById('myform').reset();
theBox=document.getElementById('chosen'); /* Note that in your code the quotes were omitted */
document.getElementById('optone').onchange=function() {
theBox.className='class'+this.value;
theBox.firstChild.nodeValue='This is class'+this.value;
}
}

window.addEventListener?
window.addEventListener('load',changeClass,false):
window.attachEvent('onload',changeClass);

</script>

</head>
<body>

<form id="myform" action="#">
<div>
<select id="optone" size="1">
<option value="0" selected="selected">-- options --</option>
<option value="1">First Choice</option>
<option value="2">Second Choice</option>
<option value="3">Third Choice</option>
</select>
</div>
</form>

<div id="chosen" class="class0">This is class0</div>

</body>
</html>


birdbrain

Fotiman

1:28 pm on Oct 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! Here's what I see:

changeClass(document.myform.optone.options[document.myform.optone.selectedIndex].value);

You are calling changeClass, passing in the value of the selected option. Then your changeClass function does this:

theBox = document.getElementById(chosen);

But, from your HTML code, chosen is going to be either " ", "1", "2", or "3", none of which are a valid "id" to be passed to getElementById. I think what you meant to do there was this:

theBox = document.getElementById("chosen");

In other words, you meant to pass the string "chosen" to getElementById. If you add quotes there, I think it will achieve what you were trying to do.

Side notes:
1. Don't use the "language" attribute on script tags. It's invalid and not needed. Instead, use the type attribute like this: <script type="text/javascript">

2. Don't include HTML comments inside your script tags.
<script type="text/javascript">
<!--
...
-->
</script>

should be simply:
<script type="text/javascript">
...
</script>

HTML comments in script tags were only needed for Netscape 1, a LOOOOOOOOONG dead browser.

3. When you declare a variable, it's good practice to use "var", otherwise you're creating a global variable (which could, in theory, cause problems with other scripts). So:
theBox = ...;

should be:
var theBox = ...;

That will keep it's scope limited to the function.

Hope that helps.

vlad_i

4:46 pm on Oct 7, 2010 (gmt 0)

10+ Year Member



Thank you guys, much appreciate the help!
I just started doing JS because I read about HTML5 and it's importance to the future of coding.

I am excellent at HTML and up to this point had others do the JS for me. Now learning it the hard way.

I will continue mocking with this tomorrow. If you guys have ever any HTML / CSS thing you can't do... I'll be glad to help. :)

V