JQuery: how to check if there is a label after input
How can I check if the label tag exists after the input tag?
I have this code
$j(document).ready(function () {
$j('input[type="checkbox"],input[type="radio"]').each(function () {
});
});
I want to check immediately after input[type:checkbox]
and input[tyoe:radio]
if there is a label mark.
If yes then do nothing, if not add empty label tag <label></label>
Does anyone know how?
+3
source to share
1 answer
You can use jQuery next () to see if the next element is a shortcut. You will get length = 0 if there is no label. Once you know if you have a label next to the input element, you can use after () to insert the element after the current element.
if(!$j(this).next('label').length)
$j(this).after('<label></label>');
+8
source to share