-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

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?

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

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.

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

Comments

0

Try something like

$(listAccordions).each(function() {
    $(this).hide();
}

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.