Has Attribute Selector [name]

attributeHas ( attribute ) version added: 1.0

  • attribute
    Type: String
    An attribute name.

Description: Selects elements that have the specified attribute, with any value.

Examples:

Example: Bind a single click that adds the div id to its text.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>css demo</title>

  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
  

<div>no id</div>
<div id="hey">with id</div>
<div id="there">has an id</div>
<div>nope</div>

<script>

$( "div[id]" ).one( "click", function() {
  var idString = $( this ).text() + " = " + $( this ).attr( "id" );
  $( this ).text( idString );
});

</script>

  
</body>
</html>

Demo: