
M4Q DataSet
Store arbitrary data associated with the matched elements or specified element.
About
M4Q contains methods for data, associated with elements. These methods allow us to associate arbitrary data with specific DOM elements.
$.hasData()
Determine whether an HTMLElement has any m4q
data associated with it. If there is no data object associated with an element, the method returns false; otherwise it returns true.
var elem = $('#elem')[0];
if ( $.hasData(elem ) {
console.log("Element has a data");
}
$.data()
Store arbitrary data associated with the specified HTMLElement and/or return the value that was set.
This is a low-level method; a more convenient $(...).data()
is also available.
$.data( document.body, "foo", 52 );
$.data( document.body, "bar", "test" );
$.removeData()
Remove a previously-stored piece of data for HTMLElement.
This is a low-level method; a more convenient $(...).removeData()
is also available.
$.removeData( document.body, "foo");
$(...).data()
Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.
$( "body" ).data( "foo", 52 );
$( "body" ).data( "bar", { isManual: true } );
$( "body" ).data( "baz", [ 1, 2, 3 ] );
console.log( $( "body" ).data( "foo" ) ); // Outputs: 52
console.log( $( "body" ).data(0) ); // Outputs: { foo: 52, bar: { isManual: true }, baz: [ 1, 2, 3 ] }
$(...).removeData()
Remove a previously-stored piece of data for the matched elements.
The .removeData()
method allows us to remove values that were previously set using .data()
.
When called with the name of a key, .removeData()
deletes that particular value.
When called with no arguments, .removeData()
removes all values.
$( "body" ).removeData( "foo" ); // remove data with key foo
$( "body" ).removeData( ); // remove all stored data