13

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?

3 Answers 3

22

You can use .data() for this.

Something like

$("#yourli").data("slidedOut", false);
Sign up to request clarification or add additional context in comments.

1 Comment

you can get the value using var value = $("#yourli").data("slidedOut");
1

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);

1 Comment

Your example won't work. You create a new object every time you call $(), so setting a property one one will not affect any others, even when it's the same element selected. Note that $('#myli')[0].slidedOut would work, but it's hardly best practice.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.