I need to select the label which is above my input and get its text.
Sometimes it is like that :
<div id="q1">
<label class="question_label">Label q1</label>
<input type ="text">
</div>
Sometimes like that:
<div id="q2">
<label class="question_label">Label q2</label>
<br>
<input type ="text">
</div>
I have tied with prev()
but sometimes there is a <br>
between the input and the label, so prev doesn't always work.
I also tried with closest()
but it doesn't return the label:
$(':input').each(function () {
alert($(this).closest('label').text());
});
What's wrong in my code ?
DO it like this: ( Tested and Verified. )
$(':input').each(function () {
alert($(this).parent().find('label').text());
});
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