jQuery Form How to - Select all inputs that are next to a label








Question

We would like to know how to select all inputs that are next to a label.

Answer


<!-- w  w w. j  a  va 2 s. c  om-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){                
            $("label + input").css("color", "blue").val("Labeled!")
        });
    </script>
  </head>
  <body>
       <form>
           <label>Name:</label>
           <input name="name" />
           <fieldset>
              <label>Age:</label>
              <input name="age" />
           </fieldset>
      </form>
    </body>
</html>

The code above is rendered as follows: