Has Attribute Selector [name]

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


attribute StringAn attribute name.

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

<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");

Looking for a Web Developer?


Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)