I have a question about selecting selectboxes with Javascript. I have upload the files on http://www.mikevierwind.nl/javascript/risicoinventarisatie.htm Here you see a table with a lot of questions and radiobuttons. Every question have 2 radiobuttons, yes and no.
When you go to the first question. “Diabetes” Than you choose for the radiobutton “ja” You see that another question will now show. But when I choose for “nee” than the question must be gone. How can I create that with the Javascript? My Javascript code is:
var diabetes = false;
var diabetesvraag = $("#blokken table.lichamelijkegezondheid .diabetesextra")
var buttondiabetes = $("#blokken table.lichamelijkegezondheid .diabetesja")
diabetesvraag.hide();
buttondiabetes.click(function()
{
if(diabetes == false)
{
diabetes = true;
diabetesvraag.show();
}
else
{
diabetes = false;
diabetesvraag.hide();
}
return false;
});
Well, first of all you need to get your HTML corrected. Each set of “yes” and “no” radiobuttons need to be grouped together with the correct
nameattribute. This will tell the browser that only one of each set of “yes” and “no” is selectable. Thenamemust be unique for each condition, however. Something like this should do:You should also use the
forattribute for thelabelelement for accessibility reasons, and try using something else other than tables for marking up this form, but that’s not relevant to the question here.Next, you can try simplifying the Javascript code using the
changeevent handler to check for both states, with something like this:You have the change the selector
$(this).parent().next('.add_qns')to suit your markup. See: http://www.jsfiddle.net/yijiang/BcAwH/ for a live demo.