Attribute existence([attr])

Description and Syntax

$('[attr]')

selects all elements that have the attr attribute, with any value.

Examples

SelectorSelects
$('[rel]')all elements that have a rel attribute
$('.myclass[style]')all elements with the class myclass that have a style attribute

The following code selects elements with id attribute.


<html>
  <head>
    <script src="http://java2s.com/style/jquery-1.8.0.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){<!--   www.j  av a 2  s.com-->
          $("div[id]").one("click", function(){
              var idString = $(this).text() + " = " + $(this).attr("id");
              $(this).text(idString);
          });
    });
    </script>
  </head>
  <body>
      Click to see.
      <div>div</div>
      <div id="hey">div</div>
  </body>
</html>

Click to view the demo

The code above generates the following result.

Attribute existence([attr])