Has Attribute Selector [name]

attributeHas selector

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

  • version added: 1.0jQuery( "[attribute]" )

    attribute: An attribute name.


Bind a single click to divs with an id that adds the id to the div's text.

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>attributeHas demo</title>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<div>no id</div>
<div id="hey">with id</div>
<div id="there">has an id</div>
// Using .one() so the handler is executed at most once
// per element per event type
$( "div[id]" ).one( "click", function() {
  var idString = $( this ).text() + " = " + $( this ).attr( "id" );
  $( this ).text( idString );


© The jQuery Foundation and other contributors
Licensed under the MIT License.