Explain Codes LogoExplain Codes Logo

Multiple radio button groups in one form

html
form-design
accessibility
angular
Anton ShumikhinbyAnton ShumikhinΒ·Sep 13, 2024
⚑TLDR

Need to juggle with multiple radio button groups within a single form? The solution lies with unique name attributes. By assigning an exclusive name to each group, they become isolated, allowing for individual selections:

<!-- Group 1: It's not just a color, it's a statement --> <input type="radio" name="color" value="Red"> Turbo Red πŸš€ <input type="radio" name="color" value="Blue"> Cool Blue πŸ’Ž <!-- Group 2: Size does matter --> <input type="radio" name="size" value="Small"> Travel Size πŸ›„ <input type="radio" name="size" value="Large"> Extra Large πŸ›οΈ

These name attributes are your secret sauce keeping the groups separate and your user experience intact.

Drawing borders: fieldset to the rescue

If you want to put your radio button groups under the spotlight, <fieldset> is your best friend. Using this tag provides both, visual edge and semantic structure, offering better accessibility for screen reader users:

<form> <fieldset> <legend>What's your color, mate?</legend> <!-- Color radio buttons... --> </fieldset> <fieldset> <legend>How big do you like it?</legend> <!-- Size radio buttons... --> </fieldset> </form>

Angular radio: Dynamic and reactive

For all the Angular devs out there, your radio button management can go dynamic. Angular's *ngFor directive facilitates dynamic generation of your button groups. Need a way to handle deselections? The (click) events are your magic wand:

<!-- Angular Template Syntax --> <div *ngFor="let group of radioGroups"> <fieldset> <legend>{{group.title}}</legend> <label *ngFor="let option of group.options"> <input type="radio" name="{{group.name}}" value="{{option.value}}" (click)="selectOption(group, option)"> {{option.label}} </label> </fieldset> </div>

Dividing and conquering form structure

Creating a well-structured and accessible form involves tactically layering your form using <div> elements and <fieldset> tags. This will sculpt your radio groups into easily navigable sections:

<form> <div class="radio-group"> <fieldset> <!-- Group 1 contents --> </fieldset> </div> <div class="radio-group"> <fieldset> <!-- Group 2 contents --> </fieldset> </div> </form>

Don't ignore the value attributes - make sure these match with their corresponding legend or label text. It's all about delivering a smooth ride for the screen reader users!

<fieldset> <legend>Choose Your Payment Method</legend> <label> <input type="radio" name="payment" value="Credit Card"> Big Spender πŸ’³ </label> <label> <input type="radio" name="payment" value="PayPal"> Secure Saver πŸ’° </label> </fieldset>

When the going gets tough: Complex scenarios

Consistent naming: Good for You, Great for Parser

While crafting your form, maintaining a consistent naming scheme for the radio button groups will make your life easier when you're parsing collected form data. Ever tried putting together a jigsaw puzzle with missing pieces? Yeah, that's how a parser feels if names are inconsistent.

Need for deselect? Script it!

Native HTML radio buttons don’t allow deselection once an option is selected. But if your form needs a "change of heart", JavaScript’s got your back. With JS, you can track button states and allow users to deselect if required.

Angular gotchas: Choose the right bindings!

Angular devs, beware of dynamic grouping issues! If your groups are dynamically generated, stick with [name] instead of [attr.name] for binding the name attribute – Angular will group your radio buttons correctly.