In the following examples, we will be using an element with the id db to store information about fruit.
Store something:
Code: Select all
// Store the current fruit
$("#db").data("fruit", "orange");
// Store an array of fruit info
$("#db").data("orange", { type: "citrus", color: "orange" } );
Code: Select all
// Find out what the current fruit is
var fruit = $("#db").data("fruit"); // orange
// Get the type of the current fruit
var type = $("#db").data("orange").type; // citrus
Code: Select all
// Remove all fruit data
$("#db").removeData("fruit").removeData("orange");