Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWeb Content Preparation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Tap Text to Affect Checkboxes and Radio Buttons

You have encountered checkboxes and radio buttons. You tap the checkbox or the radio button to change its state.

Examples:

Checkbox
Radio Button One
Radio Button Two

The checkbox can be checked or unchecked. The radio button can only be checked. To uncheck a radio button, another radio button of the same set needs to be checked.

Now, the interesting part.

The HTML label tag can be used so you can tap the label text of the checkbox or radio button and it will act as if you tapped the box or button itself.

Examples:

The text to tap doesn't need to be right next to the box or button.

Examples:

Checkbox
Radio Button Button One
Radio Button Two

Now, let's have a look at the code.

Here is the code for the first example (checkbox and radio buttons without tappable text)

<input type="checkbox"> Checkbox
<input type="radio" name="set"> Radio Button One
<input type="radio" name="set"> Radio Button Two

The above are minimum-code examples. The checkbox is not required to have a name. In order for the browser to recognize them as a set, the radio buttons need the same name.

The checkbox is checked or unchecked when it is tapped. The radio buttons are checked when tapped. When one radio button is checked, any others in the set are unchecked.

Now, let's do the code where the text labels are tappable. Tap the text label and the corresponding checkbox or radio button is affected.

<label><input type="checkbox"> Checkbox</label>
<label><input type="radio" name="set"> Radio Button One</label>
<label><input type="radio" name="set"> Radio Button Two</label>

The checkbox and radio button fields are enclosed in label tags. It's that simple.

The third and last example, though, is not that simple.

In order to specify text to tap that is at a different location than the checkbox or radio buttons:

  1. The input tag for the checkbox and radio button fields must have an id value.

  2. The label for the tappable text must have a for tag that matches the id of the field is supposed to affect.

<input id="cbox" type="checkbox"> Checkbox
<input id="rad1" type="radio" name="set"> Radio Button Button One
<input id="rad2" type="radio" name="set"> Radio Button Two

<label for="rad1">&rarr;Tap to affect the first radio button</label>
<label for="rad2">&rarr;Tap to affect the second radio button</label>
<label for="cbox">&rarr;Tap to affect the checkbox</label>

Notice that the id value of the checkbox and radio button input fields match the for value of the label tags.

An example of how it works: When the text with <label for="cbox"> is tapped, the checkbox with <input id="cbox" is affected.

Now you know how to do it. When certain text is tapped, a certain checkbox or radio button is affected.

This article first appeared with an issue of the Possibilities newsletter.

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC