having some issues with switch statement [SOLVED]
I have 3 radio buttons with values when checked of 0, 1 and 2. I'm trying to toggle 3 different divs, depending on what radio is selected. Considering it's multiple cases, I thought it would be best to use a switch statement - but the code I have below is not working. It keeps writing out the default value.
If I put a document.write line in to write out this.value right after I bind to the click event, it will output the value of the selected radio when it's clicked.
I'm guessing there is some heinous syntax error I'm missing. Any helo would be appreciated.
-
BROKEN CODE
$(document).ready(function() {
$('input[@name="system"]').click(function(){
switch ($(this).val()) {
case 0:
document.write("i");
break;
default:
document.write("x");
}
});
});
-
FIXED CODE
$(document).ready(function() {
$('input[@name="system"]').click(function(){
switch ($(this).val()) {
case '0':
document.write("i");
break;
default:
document.write("x");
}
});
});
-
Alternative code, using :eq() selector
//the following code assumes that radio buttons return a numeric
//value that is incremented by 1 for each new radio element, and
//begins with a value of 0.
$(document).ready(function() {
//hide everything that is not the first detail set
$('span.websupport_toggle:not(:first)').hide();
//bind the following function to the click event
$('input[@name="system"]').click(function(){
//set the variable 'val' to be equal to the returned value of the clicked radio
var val = this.value;
//when clicking the radio, slide up whatever is visible
$("span.websupport_toggle:visible").slideUp("slow");
//use the :eq() selector, concatenating the contents of
//the variable 'val' as the argument for :eq()
$('span.websupport_toggle:eq(' + val + ')').slideDown("slow");
});
});