3

I'm having difficulties to change class of element based on selected item in select tag. This is my code:

<table> 
    <tr>
        <select onchange="wow(this.value)">
            <option value="a">a</option>
            <option value="b">b</option>            
        </select>
    </tr>               
    <tr id="x" class="show">
        x
    </tr>
</table>

<script>
function wow(value){
    switch(value){
        case "a": document.getElementById("x").className = "show"; break;       
        case "b": document.getElementById("x").className = "hide"; break;
    }
}
</script>

<style>
.show{
    display:inline-block;
}

.hide{
    display:none;
}
</style>

I don't see any problem here. I also tried setAttribute("class", "show") but doesn't work.

shankshera
  • 947
  • 3
  • 20
  • 45

2 Answers2

8

You have to wrap X in a <td>:

http://jsfiddle.net/DerekL/CVxP7/

<tr>...<td id="x" class="show">x</td></tr>

Also you have to add case in front of "a":

case "a":
    document.getElementById("x").setAttribute("class", "show");
    break;

PS: There is a more efficient way to achieve what you are trying to do here:

http://jsfiddle.net/DerekL/CVxP7/2/

function wow(value) {
    var index = {
        a: "show",
        b: "hide"
    };
    document.getElementById("x").setAttribute("class", index[value]);
}

Now you don't need to type document.getElementById("x").setAttribute("class"... twice.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

Try this

switch(value){
    case "a": document.getElementById("x").setAttribute("class", show); break;        
    case "b": document.getElementById("x").setAttribute("class", hide); break;
}
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32