-1

I'm building an array with some elements like this :

var listAccordions = [];
$("#accordeon ul.sub-menu.ui-accordion").each(function() { 
listAccordions.push("#" + $(this).parent().attr('id') + " ul.sub-menu");
});

everything works fine here, i've got my array and it's nice, now I want to use it like this :

$( listAccordions ).hide();

but this doesn't seem to work ?

Thank you very much for your help

asked Oct 16, 2012 at 14:12

5 Answers 5

1

You can do it like this:

$(listAccordions.join(',')).hide();

But the reasonable question: why would you need to have such a data structure?

answered Oct 16, 2012 at 14:16
Sign up to request clarification or add additional context in comments.

1 Comment

ok thanks, it seems to work, well about the data structure : I need : #myid-8798 ul.sub-menu so i though i would do it directly in the array and also i need to compare it with an element that I don't need in the array and this element is already in this format... so...a better way ?
0

You will have to loop over listAccordions and call hide for each one.

for (var i = 0; i < listAccordions.length; i++) {
 $(listAccordions[i]).hide();
}
answered Oct 16, 2012 at 14:14

1 Comment

it could be a solution also, i guess the "for" solution is even faster then the "each" too...
0

listAccordions is in memory, not the DOM. You can add a class name to these elements, then use the class name to hide them - if you plan to use this collection for other things. Otherwise, hide them in the loop.

answered Oct 16, 2012 at 14:14

Comments

0
$.each(listAccordions,function(i,item) {
 $("#"+item).hide();
});

will work. Your array is an array of strings

var listAccordions = [];
$("#accordeon ul.sub-menu.ui-accordion").each(function() { 
 listAccordions.push($("#" + $(this).parent().attr('id') + " ul.sub-menu"));
});
listAccordions.each(function() {
 $(this).hide()
});

should work too

answered Oct 16, 2012 at 14:14

Comments

0

Try something like

$(listAccordions).each(function() {
 $(this).hide();
}
answered Oct 16, 2012 at 14:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.