3

Given the following form, how do I select all labels EXCEPT those immediately preceding a checkbox or radio input.

<form>
<label>Select This Label</label><input type="text" />
<label>Select This Label</label><input type="email" />
<label>Select This Label Main checkbox group</label><br>
<label>NOT THIS LABEL!</label><input type="checkbox" />
<label>NOT THIS LABEL!</label><input type="radio" />
<label>Select this arbitrary label just hanging out here</label>
</form>

Goal: I'm trying to add a class to all labels except label 4 and 5.

EDIT: The selectors I've tried use the + and :not() operators in different places. So

$( form label +:not(input[type=radio],input[type=checkbox]) ~ label) 

or

$( form label:not(+input[type=radio],+input[type=checkbox]) ~ label) 

and such.

Edit 2* I have a need to do this COMPLETELY in the selector.

Geoffrey Ochsner
  • 235
  • 3
  • 14

2 Answers2

1

Try:

$("form").find("label").each(function(){
    if(!($(this).next().is("input[type='radio']") || ($(this).next().is("input[type='checkbox']")))){
        $(this).addClass("someclass");
    }
});

Fiddle here.

codingrose
  • 15,563
  • 11
  • 39
  • 58
0

I don't think it is possible just by using a selector

$('label').filter(function(){
    return !$(this).next().is('[type="radio"], [type="checkbox"]')
})

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531