:data() selector

Selects elements which have data stored under the specified key.

The expression $( "div:data(foo)") matches a <div> if it has data stored via .data( "foo", value ).

jQuery( ":data(key)" )
Examples:

Select elements with data and change their background color.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>data demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <style>
  div {
    width: 100px;
    height: 100px;
    border: 1px solid #000;
    float: left;
  }
  </style>
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
 
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
 
<script>
$( "#one" ).data( "color", "blue" );
$( "#three" ).data( "color", "green" );
 
$( ":data(color)" ).each(function() {
  var element = $( this );
  element.css( "backgroundColor", element.data( "color" ) );
});
</script>
 
</body>
</html>
doc_jQuery
2016-03-28 14:47:49
Comments
Leave a Comment

Please login to continue.