How to assign custom property to jQuery object?

I need to assign a custom property to a jQuery object. Here is the object:

var object = $("<div id='item'></div>"); 

I need an object to have a custom data item. How can i add this?

+7
javascript jquery member
source share
2 answers
+17
source share

The .data () function allows you to store or retrieve arbitrary data and associate it with matched elements.

In many situations, it is easiest to store key-value pairs using data (), against the body tag.

Data Storage Example

 //Store the string "bar" with the body tag, with the key "foo" $('body').data('foo', 'bar'); 

Data Search Example

 //Retrieve the string "bar" var str = $('body').data('foo'); 

Link

+10
source share

All Articles