I have 2 radio button with 2 group.
The structure is like this
- Main Radio 1
- Main Radio 2
Under Main Radio 2, there’s two more sub radio button.
- Main Radio 1
- Main Radio 2
- Sub Radio 1
- Sub Radio 2
What am I doing is, in default stage, it will only show Main Radio 1 and Main Radio 2 button. When choose Main Radio 2, two sub radio button of Main Radio 2 appear.
When choose back to Main Radio 1, it will hide the list of Main Radio 2.
The one that I want to achieve is,
When click Main Radio 1, the selection that I made for Sub Radio 1 or Sub Radio 2, want to be uncheck / reset too.
I tried with this javascript, but no success.
JavaScript
x
2
1
document.getElementById("subradiobuttons").reset();
2
Please kindly help me the solutions. Thank you.
With Regards,
Advertisement
Answer
I think the best approach for a simple task like this does not needs a full JavaScript library like jQuery.
JavaScript
1
10
10
1
document.getElementById("main2").addEventListener("click", function()
2
{
3
document.getElementById("subCheckButtons").style.opacity = 1;
4
}, false);
5
document.getElementById("main1").addEventListener("click", function()
6
{
7
document.getElementById("subCheckButtons").style.opacity = 0;
8
document.getElementById("sub1").checked = false;
9
document.getElementById("sub2").checked = false;
10
}, false);
JavaScript
1
6
1
<input type="radio" id="main1" name="main" />
2
<input type="radio" id="main2" name="main" />
3
<div id="subCheckButtons" style="opacity:0;">
4
<input type="radio" id="sub1" name="sub" class="subCheck" />
5
<input type="radio" id="sub2" name="sub" class="subCheck" />
6
</div>
Or see the fiddle.