jQuery Selector How to - Select elements with the specified attribute








Question

We would like to know how to select elements with the specified attribute.

Answer


<!--from   w  w  w .j a v a 2 s .c o  m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("input[name='ab']").val("ab");
        });
    </script>
  </head>
  <body>
      <input name="ab" />
  </body>
</html>

The code above is rendered as follows: