javascript - onclick is not working with document.getElementsByClassName -


i have accordion menu using product descriptions in cms trying develop javascript/css/html speed data entry having same class name encapsulate sections , subsections. saw on website , thought cool, cannot work.

e.g sections this:

<button class="accordion_f">section 1 title</button> <div class="panel">  <button class="accordion_f">section 1 subsection title</button>  <div class="panel">  section 1 contents</div> <button class="accordion_f">section 2 title</button> <div class="panel">  <button class="accordion_f">section 2 subsection title</button>  <div class="panel">  section 2 contents</div>  , on...  section 1  section 1 contents  section 1 contents section 2   section 2 contents  section 2 contents , on 

this way can cut , paste content without worrying specific div id names.

css

    <style> button.accordion_f {     background-color: #eee;     color: #444;     cursor: pointer;     padding: 14px;     width: 100%;     border: none;     text-align: left;     outline: none;     font-size: 15px;     transition: 0.4s; }  button.accordion_f.active, button.accordion_f:hover {     background-color: #ddd;     color: blue; }  button.accordion_f:after {     content: '\02795';     font-size: 13px;     color: #777;     float: right;            margin-left: 5px;   }  button.accordion_f.active:after {     color: blue;     content: "\2796"; }  div.panel {     padding: 0 14px;     background-color: white;     max-height: 0;     overflow: hidden;     transition: 0.3s ease-in-out;     opacity: 0; }  div.panel.show {     opacity: 1;     max-height: 6000px;   } </style>  

javascript

<script> var acc = document.getelementsbyclassname("accordion_f"); var i;  (i = 0; < acc.length; i++) {     acc[i].onclick = function(){     this.classlist.toggle("active");     this.nextelementsibling.classlist.toggle("show");            } } </script> 

when tested in isolation worked correctly

if encapsulate menu in class example in pill menu onclick not work, not expand menu, css seems work, background shading etc, when inspect element not see script, parent class affecting document.getelementsbyclassname how?

<ul class="tabs"> <button class="accordion_f">section 1</button> <div class="panel"> <p>offers easy way connect polar training device 3<sup>rd</sup> party services.</p> </div> </ul> 

onclick not firing...

i have manually expanded menu's various index numbers test menus:

acc[0].classlist.toggle("active"); acc[0].nextelementsibling.classlist.toggle("show"); 

this works puzzled why onclick not working.

please help.

it working, i.e in compatibility mode , ie8 had poor support document.getelementsbyclassname


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -