How to create a checkbox with a clickable label?
Method 1: Wrap Label Tag
Wrap the checkbox within a label
tag:
<label><input type="checkbox" name="checkbox" value="value">Text</label>
Method 2: Use the for
Attribute
Use the for
attribute (match the checkbox id
):
<input type="checkbox" name="checkbox" id="checkbox_id" value="value">
<label for="checkbox_id">Text</label>
NOTE: ID must be unique on the page!
Explanation
Since the other answers don't mention it, a label can include up to 1 input and omit the for
attribute, and it will be assumed that it is for the input within it.
Excerpt from w3.org (with my emphasis):
[The for attribute] explicitly associates the label being defined with another control. When present, the value of this attribute must be the same as the value of the id attribute of some other control in the same document. When absent, the label being defined is associated with the element's contents.
To associate a label with another control implicitly, the control element must be within the contents of the LABEL element. In this case, the LABEL may only contain one control element. The label itself may be positioned before or after the associated control.
Using this method has some advantages over for
:
No need to assign an
id
to every checkbox (great!).No need to use the extra attribute in the
<label>
.The input's clickable area is also the label's clickable area, so there aren't two separate places to click that can control the checkbox - only one, no matter how far apart the
<input>
and actual label text are, and no matter what kind of CSS you apply to it.
Demo with some CSS:
label {
border:1px solid #ccc;
padding:10px;
margin:0 0 10px;
display:block;
}
label:hover {
background:#eee;
cursor:pointer;
}
<label><input type="checkbox" />Option 1</label>
<label><input type="checkbox" />Option 2</label>
<label><input type="checkbox" />Option 3</label>
How do I make a portion of a Checkbox's text clickable?
There actually is an elegant solution, using CheckBox
and single TextView
. Along with a combinations of TextView.setClickable()
, Intent Filter, and TextView.setMovementMethod()
.
You have main view (here, I called it ClickableTextViewExample):
package id.web.freelancer.example;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.CheckBox;
import android.widget.TextView;
public class ClickableTextViewExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CheckBox checkbox = (CheckBox)findViewById(R.id.checkBox1);
TextView textView = (TextView)findViewById(R.id.textView2);
checkbox.setText("");
textView.setText(Html.fromHtml("I have read and agree to the " +
"<a href='id.web.freelancer.example.TCActivity://Kode'>TERMS AND CONDITIONS</a>"));
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:clickable="true" />
</LinearLayout>
</LinearLayout>
TCActivity.java
package id.web.freelancer.example;
import android.app.Activity;
import android.os.Bundle;
public class TCActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tc);
}
}
tc.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tcView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Terms and conditions" />
</LinearLayout>
and the final piece of codes that glue it all, the AndroidManifest.xml:
<activity android:name="TCActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="id.web.freelancer.example.TCActivity" />
</intent-filter>
</activity>
Here comes, the explanations:
textView.setText(Html.fromHtml("I have read and agree to the " +
"<a href='id.web.freelancer.example.TCActivity://Kode'>TERMS AND CONDITIONS</a>"));
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
setClickable
will allow you to click ontextView
. But not the HREF link. To do that, you will have to usesetMovementMethod()
and set it toLinkMovementMethod
.
After that, you need to catch the URL. I did this using intent-filter
in AndroidManifest.xml
<action android:name="android.intent.action.VIEW" />
<data android:scheme="id.web.freelancer.example.TCActivity" />
It catch VIEW command and it only filter URL starting with
id.web.freelancer.example.TCActivity://
Here's the package for you to try it out and here's the github repository. Hope this helped
How to create an HTML checkbox with a clickable label and clickable box without the clickable whitespace
Just add a width: 25px;
to the container's style. The width should be just large enough to cover every checkbox (see snippet below):
/* Stylesheet *//* The container */
.container { display: block; position: relative; padding-left: 35px; width: 25px; /* this was added to fix the issue */ margin-bottom: 12px; cursor: pointer; font-size: 22px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none;}
/* Hide the browser's default checkbox */
.container input { position: absolute; opacity: 0; cursor: pointer;}
/* Create a custom checkbox */
.checkmark { position: absolute; top: 0; left: 0; height: 25px; width: 25px; background-color: #eee;}
/* On mouse-over, add a grey background color */
.container:hover input~.checkmark { background-color: #ccc;}
/* When the checkbox is checked, add a blue background */
.container input:checked~.checkmark { background-color: #2196F3;}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after { content: ""; position: absolute; display: none;}
/* Show the checkmark when checked */
.container input:checked~.checkmark:after { display: block;}
/* Style the checkmark/indicator */
.container .checkmark:after { left: 9px; top: 5px; width: 5px; height: 10px; border: solid white; border-width: 0 3px 3px 0; -webkit-transform: rotate(45deg); -ms-transform: rotate(45deg); transform: rotate(45deg);}
<!DOCTYPE html><html>
<body>
<h1>Custom Checkboxes</h1>
<label class="container">One <input type="checkbox" checked="checked"> <span class="checkmark"></span> </label>
<label class="container">Two <input type="checkbox"> <span class="checkmark"></span> </label>
<label class="container">Three <input type="checkbox"> <span class="checkmark"></span> </label>
</body>
</html>
How to make checkbox clickable only on checkbox?
Don't use a label, replace it with something else.
Nesting the input in the label automatically associates it with the label.
<div style="display: flex; align-items: center">
<input
type="checkbox"
style="margin-right: 12px"
class=""
checked="checked"
name="sameadr"
/>
<span class="reach-out"> Reach out to me on</span
><span class="whatsapp-icon"></span>
<span class="whatsapp-content"> Whatsapp</span>
</div>
Make check box like button and clickable
Since label is only element that can trigger input, therefor instead of li
apply button style to label
so it will look and act like button.li.nft-item-category-list label
.nft-item-category-list input[type=checkbox]+label {
margin: 0.2em 0;
cursor: pointer;
padding: 0.2em 0;
text-align: left;
font: normal normal normal 14px/14px Poppins;
color: #8E8E93;
}
.nft-item-category-list input[type=checkbox] {
display: none;
}
.nft-item-category-list input[type=checkbox]+label:before {
content: "✓";
display: none;
width: 1em;
height: 1em;
padding-left: 0.1em;
padding-bottom: 0.10em;
margin-right: 0.5em;
vertical-align: bottom;
color: transparent;
transition: .2s;
color: #000;
border-radius: 100%;
background-color: #8E8E93;
}
.nft-item-category-list input[type=checkbox]+label:active:before {
transform: scale(0);
color: #000;
}
.nft-item-category-list input[type=checkbox]:checked+label:before {
background-color: MediumSeaGreen;
border-color: MediumSeaGreen;
color: #000;
}
.nft-item-category-list input[type=checkbox]:disabled+label:before {
transform: scale(1);
border-color: #aaa;
}
.nft-item-category-list input[type=checkbox]:checked:disabled+label:before {
transform: scale(1);
background-color: #bfb;
border-color: #bfb;
}
.nft-item-category-list input[type=checkbox]:checked+label {
color: #30D158;
transition: all .2s linear;
}
ul.nft-item-categories {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
gap: 15px;
align-items: center;
}
li.nft-item-category-list label{
background: #2C2C2E 0% 0% no-repeat padding-box;
border-radius: 5px;
width: 184px;
height: 41px;
display: flex;
justify-content: center;
align-items: center;
}
li.nft-item-category-list{
list-style: none;
}
<ul class="nft-item-categories">
<li class="nft-item-category-list">
<input type="checkbox" id="cat1" name="cat1" value="Business">
<label for="cat1">Business</label>
</li>
<li class="nft-item-category-list">
<input type="checkbox" id="cat2" name="cat2" value="Animals">
<label for="cat2">Animals</label>
</li>
<li class="nft-item-category-list">
<input type="checkbox" id="cat3" name="cat3" value="Technology">
<label for="cat3">Technology</label>
</li>
<li class="nft-item-category-list">
<input type="checkbox" id="cat4" name="cat4" value="Industry">
<label for="cat4">Industry</label>
</li>
<li class="nft-item-category-list">
<input type="checkbox" id="cat5" name="cat5" value="Food">
<label for="cat5">Food</label>
</li>
</ul>
How do I make a checkbox toggle from clicking on the text label as well?
Set the CSS display
property for the label to be a block element and use that instead of your div - it keeps the semantic meaning of a label while allowing whatever styling you like.
For example:
label { width: 100px; height: 100px; display: block; background-color: #e0e0ff;}
<label for="test"> A ticky box! <input type="checkbox" id="test" /></label>
Making checkbox and radio labels clickable
No reason other than laziness. <label>
s are essential for accessibility, and are also pretty handy for those of us who have poor aim with our mouse clicks :)
Related Topics
How to Get Data from Dialogfragment to a Fragment
How to Programmatically Pair a Bluetooth Device on Android
How to Click a Button Programmatically for a Predefined Intent
Designing Android Apps for Tablets
Using Onbackpressed() in Android Fragments
Android - How to Set Background Color of All Screens
How to Enable Zoom Controls and Pinch Zoom in a Webview
Android Httpsurlconnection Eofexception
Loop Through All Subviews of an Android View
Android: Execute Code in Regular Intervals
Fragment Standard Transition Not Animating
Set a Consistent Style to All Edittext (For E.G.)
Successful Share Intent for Android
Android 4.4.2 - Java.Lang.Runtimeexception: Performing Stop of Activity That Is Not Resumed
How to Enable Autostart Option for My App in Xiaomi Phone Security App Programmatically in Android