views:

203

answers:

8

I have this code in a function, and want to shorten it - it applies the same style to every item in an array.

document.getElementById(divsArray[0]).style.visibility='hidden'; document.getElementById(divsArray[1]).style.visibility='hidden'; document.getElementById(divsArray[2]).style.visibility='hidden'; document.getElementById(divsArray[3]).style.visibility='hidden';

NO answer to date worked (Because I am looping thru the code??)

Resolved it by setting only the previously displayed slide visibility to hidden

 x=i;
 i=i+1;

 document.getElementById(divsArray[x]).style.visibility='hidden'
+7  A: 

How about using a loop:

for (var i = 0; i < 4; i++) {
    document.getElementById(divsArray[i]).style.visibility = 'hidden'; 
}
Darin Dimitrov
Any reason for the downvote? Please leave a comment when downvoting an answer.
Darin Dimitrov
First past the post gets the vote today! - Thanks
Rhys
+2  A: 
for (i=0;i<4;i++) {
  document.getElementById(divsArray[i]).style.visibility='hidden';
}
gabe3886
It's good style to declare your index variable *somewhere* (though not necessarily in the for() statement, since that doesn't create scope anyway) to avoid problems with globals. But Darin beat you to the punch this time... ;-)
Shog9
"Beaten to the punch" by all of 4 seconds (according to what I saw when the page reloaded. I didn't bother with deleting as I figured it reinforced the point that it was the way to do it.
gabe3886
+5  A: 

Just to provide something different, a jQuery solution:

$(divsArray).each(function() {
  $(this).css("visibility", "hidden");
});

Edit: It looks like you might have to collect your DOM references first. (divsArray is really just an array of div names, and not the divs themselves?)

$(divsArray).each(function() {
  $("#" + this).css({ "visibility": "hidden" });
});
sworoc
Dude, you're asking for trouble with that answer. This is a tough crowd! :) Check out the comments. :)
David Hoerster
@D Hoerster - ha ha, your not wrong I was just about to add a comment and the anwswer was deleted part way through! :)
Alex Key
if `divsArray` is all id strings, it should be `$('#'+this).css("visibility", "hidden"); ` right?
lincolnk
Yes, I added how I would do it to my answer for clarity.
sworoc
I like this code because it is really short, and because it offers the possibility of varying the number of items in the array
Rhys
+4  A: 

It sounds to me that there might be more divs... Might I suggest this change to Darin's code:

for (var i = 0; i < divsArray.length; i++) {
   document.getElementById(divsArray[i]).style.visibility = 'hidden'; 
}
It Grunt
Good point on getting the divsArray.length
Alex Key
I like this on principle, though strictly-speaking it isn't necessarily a replacement for what the OP wrote.
Shog9
I wouldn't use `divsArray.length` unless the OP explicitly says that he wants to use all the elements in the array. What if he wants only the first four elements? But it's a good point anyway.
Darin Dimitrov
@Darin OP does state every item in the array.
lincolnk
+4  A: 

And here's how it works in both Prototype and Mootools:

$(divsArray).each(function(item) {
  $(item).setStyle({visibility: "hidden"});
});
seanizer
why the downvote? I don't see where this is a duplicate...
seanizer
+4  A: 

You can put the following function in the same/descendant scope of divsArray.

function baka(idx) {
  document.getElementById(divsArray[idx]).style.visibility = 'hidden';
}

Then you can do either

baka(0);
baka(1);
baka(2);
baka(3);

or

for (var i = 0; i < 4; i++)
  baka(i);

It looks pointless, but if you have more arrays like that, you may want to modify your function like this:

function aho(arr, idx) {
  document.getElementById(arr[idx]).style.visibility = 'hidden';
}

and loop through any array like this:

for (var i = 0; i < divsArray.length; i++)
  aho(divsArray, i);

And no, there are no macros nor are there templates.

syockit
Very creative, +1 for providing an interesting answer.
sworoc
+1  A: 

as long as we're all piling on, i'll take the most direct approach :D

document.getElementById(divsArray[0]).style.visibility =
document.getElementById(divsArray[1]).style.visibility =
document.getElementById(divsArray[2]).style.visibility =
document.getElementById(divsArray[3]).style.visibility = 'hidden';

and just to go against the grain:

var d = null, i = 0;
while (d = document.getElementById(divsArray[i++])) {
    d.style.visibility = 'hidden';
}
lincolnk
+1  A: 

I couldn't "resist" to the challenge. I would say you add them the same class and do something like (Prototype example):

$$('.className').invoke('setStyle', { 'visibility' : 'hidden' });
mhitza
+1 for invoke, didn't think of that, but apart from that you are making assumptions (maybe there is not a common css class or maybe there is and it's also used by other components). So you must find a way to convert an array of ids to a map of elements first. My guess would be arrayOfIds.map($).invoke() but I am not sure.
seanizer