This is borrowed from another question I made on the site, but would have got really messy.
So using some of the code from one of the repliers, how do I get this code to work… or have I got it completely wrong.
Select Options are:
<select style="width: 220px; display: none;" id="speedB" name="category">
<option value=""<?if ($category == ""){?> selected="selected"<?}?>>Please Select</option>
<option value="1"<?if ($category == "1"){?> selected="selected"<?}?>>Pest Inspectors</option>
<option value="3"<?if ($category == "3"){?> selected="selected"<?}?>>Building Inspectors</option>
<option value="2"<?if ($category == "2"){?> selected="selected"<?}?>>Removalists</option>
<option value="5"<?if ($category == "5"){?> selected="selected"<?}?>>Conveyancing</option>
<option value="4"<?if ($category == "4"){?> selected="selected"<?}?>>Pool Inspectors</option>
</select>
JavaScript:
$('#speedB').change(function() {
var myTypes = {
'': '.nothing',
'1': '.pest',
'2': '.build',
'3': '.removals',
'4': '.legal',
'5': '.pool'
}
var selectedType = myTypes[$(this).val()];
$('#topright').html('<div class="'+selectedType+'"/></div>');
});
HTML:
<div id="topright"></div>
CSS:
#topright {
height: 70px;
position: absolute;
right: 0;
top: 0;
width: 70px;
}
.pest {
background: url("images/pestcorner.png") no-repeat scroll 0 0 transparent;
}
.build {
background: url("images/buildcorner.png") no-repeat scroll 0 0 transparent;
}
.removals {
background: url("images/removalscorner.png") no-repeat scroll 0 0 transparent;
}
.legal {
background: url("images/legalcorner.png") no-repeat scroll 0 0 transparent;
}
.pool {
background: url("images/poolcorner.png") no-repeat scroll 0 0 transparent;
}
Essentially, I want user to select from select box, and that invokes display of the css class.
I think I have screwed the js up.
I ‘d do it like this:
First, add HTML data attributes with the corresponding class name for each option:
This way the relationships between CSS class and selected option are nicely prominent.
You can then do:
Apart from that, you got the solution yourself (extra “.” in the name of the class being added).