Supriyo Karmakar
Supriyo Karmakar

Reputation: 11

Unable to understand conditional IF statement code

Following code is written for toggle-visibility of a submit button. Can you explain the conditional IF statement?

function ToggleVisibility(){
  var b ='btn_Submit,btn_Abort_Master';
  var temp = new Array();
      temp = b.split(',');
  var el;var i=0;
  for(i=0;i<temp.length;i++){
      try{
             el = document.getElementById(temp[i]);
      }catch(e){
          try{
             el = document.all[temp[i]];
          }catch(e){}
      }
      if (el && (el = el.style) && ('string' == typeof el.visibility){
           el.visibility = ('hidden' == el.visibility ? 'visible' :'hidden');
      }
  }
  return true;
}

Upvotes: 0

Views: 52

Answers (3)

Akki619
Akki619

Reputation: 2432

 function ToggleVisibility(){

  var b ='btn_Submit,btn_Abort_Master';    

above are element on your web page taken in string (eleemnt id's)

 var temp = new Array();
      temp = b.split(',');
  var el;var i=0;

taking an array object and splitting variable b by comma so that you will get btn_Submit,btn_Abort_Master in array. So temp[0] holds first value and temp[1] second value which are element id where toggle needs to be checked and applied.

 for(i=0;i<temp.length;i++){
      try{
             el = document.getElementById(temp[i]);
      }catch(e){
          try{
             el = document.all[temp[i]];
          }catch(e){}
      }
      if (el && (el = el.style) && ('string' == typeof el.visibility){
           el.visibility = ('hidden' == el.visibility ? 'visible' :'hidden');
      }
  }
  return true;
}

looping through the temp array length and getting the element object in el. At end we are checking if element obj is present, if ele object has a style and ele object type. Inside if we are toggling visibility of element if hidden the visible and vice-versa.

Upvotes: 0

Oleksii Artiukh
Oleksii Artiukh

Reputation: 384

el - if this variable is declared return true
el = el.style - if you can to replace el with el.style return true
'string' == typeof el.visibility - if el.visibility is a string return true

if (el && (el = el.style) && ('string' == typeof el.visibility) - if all of these statements are true do el.visibility = ('hidden' == el.visibility ? 'visible' :'hidden');

Upvotes: 0

Black0ut
Black0ut

Reputation: 1737

Its checks the following conditions :

  1. that the variable el has value (meaning that the document.getElementById(temp[i]) or document.all[temp[i]] method call returned an element)
  2. That the element el has a defined style property
  3. that the style.visibility property value's type is a string

Upvotes: 1

Related Questions