Javascript shortcut for duplicating strings

I have this code in a function and you want to shorten it - it applies the same style to every element in the 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'; 

There is NO answer on the date of work (Because I am sorting through the code?)

Solved by setting only hidden hidden visibility of the slide

 x = i; i = i+1; document.getElementById(divsArray[x]).style.visibility = 'hidden'; 
+7
javascript arrays
source share
9 answers

How about using a loop:

 for (var i = 0; i < 4; i++) { document.getElementById(divsArray[i]).style.visibility = 'hidden'; } 
+8
source share

Just to provide something else, a jQuery solution:

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

Edit: It looks like you will first need to compile your DOM links. (divsArray is just an array of div names, not the divs themselves)?

 $(divsArray).each(function() { $("#" + this).css({ "visibility": "hidden" }); }); 
+5
source share

It seems to me that there might be more div ... Can I suggest this change for Darin's code:

 for (var i = 0; i < divsArray.length; i++) { document.getElementById(divsArray[i]).style.visibility = 'hidden'; } 
+4
source share

And here, how it works in both Prototype and Mootools :

 $(divsArray).each(function(item) { $(item).setStyle({visibility: "hidden"}); }); 
+4
source share

You can put the following function in the same / threaded area of ​​divsArray.

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

Then you can do

 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 of these arrays, you can change your function as follows:

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

and skip any array like this:

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

And no, there are no macros and no templates.

+4
source share
 for (i=0;i<4;i++) { document.getElementById(divsArray[i]).style.visibility='hidden'; } 
+2
source share

While we are all stuffing, I will use 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 go against the grain:

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

I could not "resist" this problem. I would say that you add them to one class and do something like (prototype example):

 $$('.className').invoke('setStyle', { 'visibility' : 'hidden' }); 
+1
source share

We can Array.prototype.forEach() over an array containing id using Array.prototype.forEach() and ES6 Arrow Functions :

 var elemIds = ['two', 'four', 'six']; elemIds.forEach(id => {document.getElementById(id).style.visibility = 'hidden';}); 
 <div id="one">One</div> <div id="two">Two</div> <div id="three">Three</div> <div id="four">Four</div> <div id="five">Five</div> <div id="six">Six</div> <div id="seven">Seven</div> 
0
source share

All Articles