0

Let's say I have 3 objects in an array:

var redBoxx=document.getElementById("redBox");
var blueBoxx=document.getElementById("blueBox");
var orangeBoxx=document.getElementById("orangeBox");

var shapeArray = [redBoxx, blueBoxx, orangeBoxx];

I want to grab an object from the array based on it's current visibility status (as in visibility: "hidden" or "visible"). How do I do that?

4
  • What metric are you using to determine "visibility status"? Commented Feb 5, 2016 at 19:51
  • You can use jQuery with a handy selector or you call loop through all the ids you have and build a new array with only those elements. Commented Feb 5, 2016 at 19:52
  • I'd suggest jQuery as well for this: it's very easy to achieve this using is(":visible"). Commented Feb 5, 2016 at 19:55
  • Will only one object be visible at a time or do you need to deal with the results as a set? Commented Feb 5, 2016 at 20:02

3 Answers 3

1

Try using something like below

var redBox = document.getElementById('redBox');
var blueBox = document.getElementById('blueBox');
var orangeBox = document.getElementById('orangeBox');

var list = [redBox, blueBox, orangeBox];
Array.prototype.filterByProp = function(prop, value){
  var currentStyle = window.getComputedStyle;
	return this.filter(function(el){return value === currentStyle(el,null)[prop]});
}
//list.filter(function(el){return window.getComputedStyle(el,null).visibility === 'visible';})
console.log('hidden:' , list.filterByProp('visibility','hidden'));
console.log('Visible:' , list.filterByProp('visibility','visible'));
div{
  box-sizing:border-box;
  width:calc(100%/3);
  height: 50vh;
  border:1px solid #000;
  float:left;
}
#redBox{
  background:#f00;
}
#blueBox{
  background:#00f;
  visibility:hidden;
}
#orangeBox{
  background:#f60;
  }
}
<div id="redBox">
</div>
<div id="blueBox">
</div>
<div id="orangeBox">
</div>

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

Comments

1

There's not a "quick" way to do this in vanilla Javascript. Your best bet is with a loop:

function visibleElementsIn(elements){
  var output = [];
  elements.forEach(function(element){
    var visibility = window.getComputedStyle(element).visibility;
    if(visibility === "visible"){
      output.push(element);    
    }
  });
  return output;
}

Comments

1

You can do this by checking the computed value of the different properties that change an element's visiblity:

Example here: https://jsfiddle.net/tt1s5mpm/

    function isVisible(el)
    {
       var styles = getComputedStyle( el );

       //Three different things are used for visibility
       if ( styles.getPropertyValue( "visibility" ) !== "hidden" &&
          styles.getPropertyValue( "display" ) !== "none" &&
          parseInt( styles.getPropertyValue( "opacity" ) ) !== 0 //You should really check all the versions like "-webkit-", etc
       )
       {
        return true;
       }

       return false;
    }

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.