tree em
tree em

Reputation: 21761

Javascript loop continue,or break to display?

Javascript loop continue,or break to display?

This is my code use for show or hide some data in table

if (type=='SHOW') {
  for(var i = 0; i<list_tr.length; i++) {
             document.getElementById(list_tr[i]).style.display = '';
  }
else if (type=='SHOW_EXCEPT'){
  document.getElementById(list_tr[0]).style.display = 'none';
  document.getElementById(list_tr[1]).style.display = '';
    for(var i = 2; i<list_tr.length; i++) { 
        document.getElementById(list_tr[i]).style.display = 'none';
     }
}else{
    for(var i = 0; i<list_tr.length; i++) { 
        document.getElementById(list_tr[i]).style.display = 'none';
  }
}

In else case:

I want to hidden all but show list_tr[2] iftype=='SHOW_EXCEPT'

How can I do in this case?

thanks

Upvotes: 0

Views: 542

Answers (2)

Mathias Bynens
Mathias Bynens

Reputation: 149704

Not sure I understand the question correctly, but it looks like you need else if:

if (type == 'SHOW') {
  // Do something
} else if (type == 'SHOW_EXCEPT') {
  // Do something else
} else {
  // Do something different
}

Upvotes: 1

Andrew
Andrew

Reputation: 5277

if (type=='SHOW') 
{   
  for(var i = 0; i<list_tr.length; i++) 
    {              
      document.getElementById(list_tr[i]).style.display = '';   
    }  
}
else
{     
  for(var i = 0; i<list_tr.length; i++) 
  {
    if (i==2||type=='SHOW_EXCEPT')
      document.getElementById(list_tr[i]).style.display = '';
    else   
      document.getElementById(list_tr[i]).style.display = 'none';
  } 
}

That should do the trick if I have understood the question correctly

Upvotes: 1

Related Questions