I'm working with some server code that is generating html that looks something like:
<input type="radio" />
<label>something</label>
<input type="radio" />
<label>something</label>
<input type="radio" />
<label>something</label>
<input type="radio" />
<label>something</label>
I want to wrap each pair in a span but I can't figure out a way to select pairs of elements on jquery in order to use wrapAll()
on them. I can't change the html that I am working with. Can anyone help?
Two selectors: visible and: hidden are also available in JQuery.
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
CSS selector is selecting all p elements in page.
$('input').each(function(){
$(this).next('label').add(this).wrapAll('<span>');
});
next
will find the closest sibling element.add
will add the matched item to the collection.Demo
You can try this.
$('input').each(function(){
$(this).next().andSelf().wrapAll('<span>');
});
$("input+label") might well be helpful.
http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With