jQuery.data() namespace

html, javascript, jquery

Solution

Why not using

$(el).data('myplugin.foo')

and

$(el).data('myplugin.xyz')

?

So if you don't need to access more than one value at the same time, you avoid useless indirections and tests.

Problem

How can I avoid conflicts with other jQuery plugins using `$.data()`? I was thinking I could use a single key to store my data like `$(el).data('myplugin', { foo: 'a', xyz: 34});` and access it like `$(el).data('myplugin').foo` etc. But how can easily change a value, without overriding the whole data? Like changing the value of "foo".

Original source