/jQuery UI

:data() Selector

data selector

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

  • jQuery( ":data(key)" )

    key: The data key.

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


Select elements with data and change their background color.

<!doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>data demo</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
  div {
    width: 100px;
    height: 100px;
    border: 1px solid #000;
    float: left;
  <script src="//code.jquery.com/jquery-1.12.4.js"></script>
  <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
$( "#one" ).data( "color", "blue" );
$( "#three" ).data( "color", "green" );
$( ":data(color)" ).each(function() {
  var element = $( this );
  element.css( "backgroundColor", element.data( "color" ) );


© The jQuery Foundation and other contributors
Licensed under the MIT License.