baklap
baklap

Reputation: 2173

jquery, add variable to element?

I have a question regarding jQuery. I have a li that slides out, now I want to remember if it has slided out so I want to set a boolean variable slidedOut.

Is it possible to simply add it to the element? Or should I add a hidden div or something to to element?

Upvotes: 13

Views: 15206

Answers (3)

Kobi
Kobi

Reputation: 138017

Another option is to use CSS classes. This is particularity useful if you don't really save data (just a flag), or want to change the design for these <li>s or their children.

Upvotes: 1

Aliostad
Aliostad

Reputation: 81660

Using jquery data is considered the standard way to do it - although there are tons of other ways to do it.

An alternative is simply:

 $("#myli")[0].slidedOut = true;
 alert($("#myli")[0].slidedOut);

Upvotes: 1

rahul
rahul

Reputation: 187040

You can use .data() for this.

Something like

$("#yourli").data("slidedOut", false);

Upvotes: 22

Related Questions