Has Attribute Selector [name] in JQuery

 

Sample:

version added: 1.0jQuery(‘[attribute]’)

  • attribute
    An attribute name.

Example:

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

<!DOCTYPE html>
<html>
<head>
  <script src="/scripts/jquery-1.4.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>

Leave a Reply

Your email address will not be published. Required fields are marked *