Closest() returning the closest label to my input - Javascript jQuery

Javascript examples for jQuery:Form Input

Description

Closest() returning the closest label to my input

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
  <script>
    $(document).ready(function () {
        $(':input').each(function () {
            console.log($(this).closest('div#q2').find('.question_label').text());
        });/*from w  w w  . j ava2 s  .c om*/
    });

      </script> 
 </head> 
 <body> 
  <div id="q2"> 
   <label class="question_label">Label q2</label> 
   <br> 
   <input type="text"> 
  </div>  
 </body>
</html>

Related Tutorials