Sunday, November 4, 2012

Using .data to attach (key,value) on any element, or jquery object

Using .data to attach (key,value) on any element, or jquery object
Purpose: To save data for later use, by callback functions
(e.g., knowing which control called this function, so that you know which control to update the text of)


$('#select-choice-custom-button').data('foo', 52); //place/save data
asdf = $('#select-choice-custom-button').data('foo') //retrieve/load data



CAVEAT:

//console.log($('#select-choice-custom-button').data()); //prints too many things; forgot to put a .data('keyname');



EXAMPLE USAGE:
$('#select-choice-custom-button').data('foo', 52);
alert($('#select-choice-custom-button').data('foo'));
console.log('ok');
 
   











No comments:

Post a Comment