jQuery attribute existence selector

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(){<!--  w  w w .  j  av a2  s  .co  m-->
          $("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





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities