JSFiddle - React, Tailwind, and code Playground
by devy indriyani
September 11, 2014
HTML
<div>A div</div>
<button>Get "blah" from the div</button>
<button>Set "blah" to "hello"</button>
<button>Set "blah" to 86</button>
<button>Remove "blah" from the div</button>
<p>The "blah" value of this div is <span>?</span></p>
CSS
body {
margin-top:10px;
padding:10px;
}
div {
margin: 5px;
background: yellow;
}
button {
margin: 5px;
font-size: 14px;
}
p {
margin: 5px;
color: blue;
}
span {
color: red;
}
JavaScript
$( "button" ).click( function() {
var value,
div = $( "div" )[ 0 ];
switch ( $( "button" ).index( this ) ) {
case 0 :
value = jQuery.data( div, "blah" );
break;
case 1 :
jQuery.data( div, "blah", "hello" );
value = "Stored!";
break;
case 2 :
jQuery.data( div, "blah", 86 );
value = "Stored!";
break;
case 3 :
jQuery.removeData( div, "blah" );
value = "Removed!";
break;
}
$( "span" ).text( "" + value );
});