In this case, I suggest you use a switch
, something like (just implement your own logic):
$('.submit4').click(function (e) {
e.preventDefault();
//check correct answers from var above
var result;
switch($('input[name=q4]:checked').attr('class')){
case 'grey':
result = "it's grey";
break;
case 'black':
result = "it's black";
break;
case 'white':
result = "it's white";
break;
case 'red':
result = "it's red";
break;
default:
result = 'Correct answer!';
break;
}
alert(result);
});
In this case the switch works by getting the class of the selected checkbox, and you can do whatever you want on each case instead of using a bunch of if/elses...
Jsfiddle demo: http://jsfiddle.net/darkajax/q4rJ8/