Easy Tutorial
❮ Jqueryui Widget Extend Ref Effects ❯

jQuery UI API - :data() Selector

Category

Selectors | UI Core

Usage

Description: Selects elements that have data stored under the specified key.

Parameter Description
key The key of the data.

The expression $( "div:data(foo)") matches a div that has data stored via .data( "foo", value ).

Example

Select elements with data and change their background color.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>:data() Selector Example</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.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.10.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>

View Demo

❮ Jqueryui Widget Extend Ref Effects ❯