Has Attribute Selector [name]

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

.attributeHas(attribute)

attribute StringAn attribute name.

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

JS
<div>no id</div>
<div id="hey">with id</div>
<div id="there">has an id</div>
<div>nope</div>
HTML
// 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);
});
DEMO

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 :)