0

I'm trying to clean up my code and was curious if it's possible to create a variable that contains other variables.

Pretty much I'm trying to turn this...

// variables are elements on a page
var1.show();
var2.show();
var3.show();
var4.hide();
var5.hide();

Into this...

var showViewVariables = (var1, var2, var3);
var hideViewVariables = (var4, var5);

showViewVariables.show();
hideViewVariables.hide();

Not sure if this fails to work due to my syntax or if its even capable.

I've found some answers where multiple selectors are placed in a variable and called together but haven't found much of anything with variables inside one variable.

2
  • what is var1, var2? are they string selectors or jquery objects? Commented May 19, 2015 at 18:50
  • They are selectors... so a div on the page that I'm trying to hide or show. Commented May 19, 2015 at 18:51

1 Answer 1

2

You can use .add()

Create a new jQuery object with elements added to the set of matched elements.

Example

var var1 = $('#one');
var var2 = $('#two');

//Create variable from another variables
var var3 = var1.add(var2);

var3.show();

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

change your fiddle to use a different jQuery version rather than compat and it will work
Im guessing this is the closest that we can get to what the OP wnats

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.