Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9037305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:12:59+00:00 2026-06-16T09:12:59+00:00

function CheckavailOnload() { var elems = document.getElementsByClassName(‘box-collateral box-related’); for(var i = 0; i <

  • 0
function CheckavailOnload()
{
  var elems = document.getElementsByClassName('box-collateral box-related');
  for(var i = 0; i < elems.length; i++){
  if(elems.style.visibility == 'visible')
  {
     var av = document.getElementsByClassName('availability in-stock');
     for(var i = 0; i < elems.length; i++) {
     av[i].style.visibility = 'visible';
    }
  }
  else
  {
     var av = document.getElementsByClassName('availability in-stock');
     for(var i = 0; i < elems.length; i++) {
     av[i].style.visibility = 'hidden';
    }
  }
 }
}
CheckavailOnload();

here i am trying to check the visibility of div “box-collateral box-related” if it is visible i want to toggel the visibility of another paragraph with class name “availability in-stock”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:13:00+00:00Added an answer on June 16, 2026 at 9:13 am

    getElementsByClassName() always gives you a HTMLCollection object, even if it had only one member. HTMLCollection object doesn’t have style property, hence you need to iterate elems in the first if to check the visibility, just like you’ve done further in your code.

    If you’re sure there’s only one member, you can check it’s visibility by using elems[0].style.visibility.

    If you want to check the visibility of a particular element, you can give it an id and get that element using document.getElementById().


    EDIT

    Thanks for a fiddle, now we can have some results.

    So, maybe you don’t need that id, the actual problem occurs when trying to get style, if it’s not explicitely assigned. To tackle this, you need to use getComputedStyle():

    function CheckavailOnload() {
    var elems = document.getElementsByClassName('box-collateral box-related');
        for (var i = 0; i < elems.length; i++) {
            if (getComputedStyle(elems[i]).visibility == 'visible' || getComputedStyle(elems[i]).display == 'block') {
                alert("hello");
                var av = document.getElementsByClassName('availability in-stock');
                for (var j = 0; j < av.length; j++) {
                    av[j].style.visibility = 'visible';
                }
            }
            else if (getComputedStyle(elems[i]).visibility == 'hidden' || getComputedStyle(elems[i]).display == 'none') {
                var av = document.getElementsByClassName('availability in-stock');
                for (var k = 0; k < av.length; k++) {
                    av[k].style.visibility = 'hidden';
                }
            }
        }
    }
    window.onload = CheckavailOnload;
    

    This code will check all elements assigned to classes box-collateral box-related. A working demo at jsFiddle.

    Notice also use of window.onload, which makes sure, that you’re not trying to get elements before they are parsed. I switched elems to av in for...j– and for...k -loops, supposed to work correctly, if there were different number of elements in elems and av.

    If the first found element with maintioned classes is the one to check, you can simply replace i with 0 in all elems[i] expressions.

    If you want to check only a particular element, you can give it an id, and get a reference to it with getElementById(). In HTML:

    <div id="checkThisOnly" class="box-collateral box-related">
    

    And then in the script:

    var elem = document.getElementById('checkThisOnly');
    if (getComputedStyle(elem).visibility...) {
        ......
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function CheckavailOnload() { var elems = document.getElementsByClassName('box-collateral box-related'); var av = document.getElementsByClassName('availability in-stock'); var
function validateRequiredFields(formid) { var inputs = document.getElementsByClassName('required'); for(i=0;i<inputs.length;i++) { var FieldID = inputs[i].id; if(
function moreFields() { counter++; var newFields = document.getElementById(input).cloneNode(true); newFields.id = counter; newFields.style.display = 'block';
function getWindowsUserName() { var WinNetwork = new ActiveXObject(WScript.Network); var urlToSite = createCustomURL(WinNetwork.UserName); document.getElementById(psyncLink).src =
function getUsername() { var userName = document.form.screen_name.value; document.getElementById(display).innerHTML = userName; var apiName = https://api.twitter.com/1/users/lookup.json?screen_name=
function trim(str,options){ var string = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length, j
function saveData() { var rows = $('#deviceTable tr').length; var toSave = ; alert(rows); for(var
function levenshtein(a, b) { var i,j,cost,d=[]; if (a.length == 0) {return b.length;} if (b.length
function ExChgClsName(Obj,NameA,NameB){ var Obj=document.getElementById(Obj)?document.getElementById(Obj):Obj; Obj.className=Obj.className==NameA?NameB:NameA; } <a href=javascript:showMenu(2);> i am a newbie of the
function loadjscssfile(filename, filetype) { if (filetype==css) { var fileref = document.createElement(link); fileref.rel = stylesheet;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.