0

I am having html like this:

<form>
    <div class="row">
        <input type="radio" class="radio">
        <label>Text</label>
        <input type="radio" class="radio">
        <label>Type</label>
    </div>
</form>

Now I need to apply a class to each label immediate after each <input type="radio">.

I am using jquery like this:

if($('input').hasClass('radio')){
    $(this).next().addClass('radio-url');
}

I am trying to add class 'radio-url' to each <label> immediately after radio tag. What mistake have I did in this?

1
  • 1
    Use $('.radio').next('label').addClass('radio-url'); - I'm guessing you don't have the correct context of this, which is why your code doesn't work - fiddle Commented Oct 29, 2013 at 15:26

9 Answers 9

3

You can use .siblings()

$('input[type="radio"]').siblings('label').addClass('radio-url');

DEMO

Or

$('input[type="radio"]').next('label').addClass('radio-url');

DEMO2

Sign up to request clarification or add additional context in comments.

1 Comment

You probably want to use .next() because siblings() will target all the labels, not just the one.
1

You can use next() function

 $("input:radio").next('label').addClass("radio-url");`

Comments

1

Try:

$(':radio').next().addClass('radio-url');

jsFiddle example (I threw a text input in there so you can see that it works on only radio inputs)

Comments

1

This answer doesn't directly answer your question!

I believe that your label should have the for attribute so that the label is associated with the radio button. This allows the user:

  1. To check the radio button by clicking the label!
  2. If the input type is text, clicking the label focuses the text input
  3. For accessibility reasons

HTML

<div class="row">
    <input type="radio" class="radio" id="text"></input>
    <label for="text">Text</label>
    <input type="radio" class="radio" id="type"></input>
    <label for="type">Type</label>
</div>

JQuery

Search the label using the radio element's ID.

$('input[type="radio"]').each(function(){
    var radioId = $(this).attr("id");
    $("label[for='" + radioId + "']").addClass('radio-url');
});

Fiddle: http://jsfiddle.net/78zAB/

Comments

0

DEMO

$('input.radio').each(function () {
    $(this).next().addClass('radio-url');
})

Comments

0

Use CSS element+element selector:

$('input:radio + label').addClass('theClass')

http://jsfiddle.net/ttnUU/

Comments

0

Works optimal

$('input[type=radio]').next('label').addClass('radio-url');

http://jsfiddle.net/q76FJ/

Comments

0

You need to loop through all radio buttons:

$('input[type="radio"]').each(function(){
    $(this).next().addClass('radio-url');
});

Or

$("input[type='radio'] + label").addClass('radio-url')

Fiddle Example

Example 2

2 Comments

The if statement is probably not needed here, nor is it even correct.
@RocketHazmat Yeah Thanks for pointing out! forgot to remove :)
0

Posting this as an answer as all of the others are using the incorrect selector, or don't specify the label in next():

$('input.radio').next('label').addClass('radio-url');

.. which will add the class radio-url to all label elements which come immediately after an input with the class radio

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.