-1

I'm trying to make a function run several div's in a Q&A accordion, but I can't figure out the right syntax for it to happen. In line 6, the classname 'questionV1' does the job well, but I want the function to run classnames 'questionV2' and 'questionV3' as well. I have tried to add questionV2 + V3, in the same line like this (divs[no].classname=='questionV1, questionV2, questionV3') but it does not work. The javascript looks like this:

function initShowHideDivs()
{
 var divs = document.getElementsByTagName('DIV');
 var divCounter = 1;
 for(var no=0;no<divs.length;no++){
  if(divs[no].className=='questionV1'){
   divs[no].onclick = showHideContent;
   divs[no].id = 'dhtmlgoodies_q'+divCounter;
   var answer = divs[no].nextSibling;
   while(answer && answer.tagName!='DIV'){
    answer = answer.nextSibling;
   }
   answer.id = 'dhtmlgoodies_a'+divCounter;
   contentDiv = answer.getElementsByTagName('DIV')[0];
   contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px';
   contentDiv.className='answer_content';
   contentDiv.id = 'dhtmlgoodies_ac' + divCounter;
   answer.style.display='none';
   answer.style.height='1px';
   divCounter++;
  }
 }
}
window.onload = initShowHideDivs;
K.L.S
  • 1
  • 1
  • Possible duplicate of [Change an element's class with JavaScript](http://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript) – Jared Smith Jan 22 '16 at 12:51

1 Answers1

0

Here are two solution for your problem. One with regex, the other with string comparison.

RegEx Solution:

  for(var index in divs){
    var div = divs[index];
    if(/questionV[123]/.test(div.className)) {

      // code here
    }
  }

String comparison:

  for(var index in divs){
    var div = divs[index];
    if(div.className === 'questionV1'
      || div.className === 'questionV2'
      || div.className === 'questionV3') {

      // code here
    }
  }

also a link to jsfiddle

Rene
  • 331
  • 7
  • 16