I have the following code:
$(document).ready(function(){
$("[name=q58]").each(function (i) {
$(this).click(function () {
var selection = $(this).val();
alert('Q58 Value = '+selection)
if (selection == '1') {
$("#region").show();
$("[name=q59]").each(function (i) {
$(this).click(function () {
var selection = $(this).val();
alert('Q59 Value = '+selection)
if (selection == '1') {
$("#location").show();
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#east, #north, #scotland, #southeast, #southwest").hide();
$("#central").show();
}
else if (selection == '2') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #north, #scotland, #southeast, #southwest").hide();
$("#east").show();
}
else if (selection == '3') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #scotland, #southeast, #southwest").hide();
$("#north").show();
}
else if (selection == '4') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #southeast, #southwest").hide();
$("#scotland").show();
}
else if (selection == '5') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southwest").hide();
$("#southeast").show();
}
else if (selection == '6') {
$("#location").show();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast").hide();
$("#southwest").show();
}
else {
$("#location").hide();
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast, #southwest").hide();
}
});
});
}
else if (selection == '10') {
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#central, #east, #north, #scotland, #southeast, #southwest").hide();
$("#location").hide();
$("#region").show();
}
else {
$("input:radio[name^='q59']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='central-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='east-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='north-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='scotland-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southeast-']").each(function(i) {
this.checked = false;
});
$("input:radio[name^='southwest-']").each(function(i) {
this.checked = false;
});
$("#region").hide();
$("#location").hide();
$("#central").hide();
}
});
});
});
Working, or rather not so working, example in JsFiddle: See example in JsFiddle: http://jsfiddle.net/ExPUu/1/
The following odd behaviour occurs:
The user selects Aftersales, they then see q59 and can select either Central or East and then see q60 and choose a further option. This works fine.
If the user selects Bus, Coach and Engines they don't get any further options - again works fine.
The odd behaviour is when the user selects Sales if they select this, they should be able to then select Central or East - if they have clicked Sales and nothing else, this works fine. BUT if they had selected Aftersales and then changed their mind and selected Sales and then select either Central or East - it then displays q60 and it shouldn't.
I've been looking through my jQuery code and cannot see where I am going wrong. Or is this an oddity?