jQuery selector for the label of a checkbox

JqueryCheckboxJquery Selectors

Jquery Problem Overview


<input type="checkbox" name="filter" id="comedyclubs"/>
<label for="comedyclubs">Comedy Clubs</label>

If I have a check box with a label describing it, how can I select the label using jQuery? Would it be easier to give the label tag an ID and select that using $(#labelId) ?

Jquery Solutions


Solution 1 - Jquery

This should work:

$("label[for='comedyclubs']")

See also: http://docs.jquery.com/Selectors/attributeEquals#attributevalue">Selectors/attributeEquals - jQuery JavaScript Library

Solution 2 - Jquery

$("label[for='"+$(this).attr("id")+"']");

This should allow you to select labels for all the fields in a loop as well. All you need to ensure is your labels should say for='FIELD' where FIELD is the id of the field for which this label is being defined.

Solution 3 - Jquery

This should do it:

$("label[for=comedyclubs]")

If you have non alphanumeric characters in your id then you must surround the attr value with quotes:

$("label[for='comedy-clubs']")

Solution 4 - Jquery

Another solution could be:

$("#comedyclubs").next()

Solution 5 - Jquery

Thanks Kip, for those who may be looking to achieve the same using $(this) whilst iterating or associating within a function:

$("label[for="+$(this).attr("id")+"]").addClass( "orienSel" );

I looked for a while whilst working this project but couldn't find a good example so I hope this helps others who may be looking to resolve the same issue.

In the example above, my objective was to hide the radio inputs and style the labels to provide a slicker user experience (changing the orientation of the flowchart).

You can see an example here

If you like the example, here is the css:

.orientation {  	position: absolute;	top: -9999px;	left: -9999px;}
    .orienlabel{background:#1a97d4 url('http://www.ifreight.solutions/process.html/images/icons/flowChart.png') no-repeat 2px 5px; background-size: 40px auto;color:#fff; width:50px;height:50px;display:inline-block; border-radius:50%;color:transparent;cursor:pointer;}
    .orR{	background-position: 9px -57px;}
    .orT{	background-position: 2px -120px;}
    .orB{	background-position: 6px -177px;}
    
    .orienSel {background-color:#323232;}

and the relevant part of the JavaScript:

function changeHandler() {
	$(".orienSel").removeClass( "orienSel" );
	if(this.checked) {
	    $("label[for="+$(this).attr("id")+"]").addClass( "orienSel" );
	}
};

An alternate root to the original question, given the label follows the input, you could go with a pure css solution and avoid using JavaScript altogether...:

input[type=checkbox]:checked+label {}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarwynView Question on Stackoverflow
Solution 1 - JqueryKipView Answer on Stackoverflow
Solution 2 - JqueryHanky PankyView Answer on Stackoverflow
Solution 3 - JqueryDarko ZView Answer on Stackoverflow
Solution 4 - JqueryBrigantiView Answer on Stackoverflow
Solution 5 - JqueryPete - iCalculatorView Answer on Stackoverflow