Attribute equals ([foo=bar])

Description and Syntax

$('[name=theValue]')

selects all elements that have the 'name' attribute with a value exactly equal to theValue.

Examples

SelectorSelects
$('[name=myname]')all elements that have a name value exactly equal to myname
$('a[rel=nofollow]')all <a> elements that have a rel value exactly equal to nofollow

The following code finds all inputs with name 'ab'.


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

  </head>
  <body>
    <body>
      <input name="ab" />
    </body>
</html>

Click to view the demo

The code above generates the following result.

Attribute equals ([foo=bar])

Use two attribute selectors together

The following code does the matching with two attribute names.


<html>/*  ww w .  ja v a2s  .c o  m*/
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("input[id][name='data']").val("data");
    });
    </script>
  </head>
  <body>
      <input id="end-data" name="data" />
  </body>
</html>