mardi 18 décembre 2018

jQuery: Appending multiple class names to element from a conditional statement

I am trying to make a shorthand conditional statement to append found classes to an element. My code is below:

<ul class="options-container class1 class2 class3-ul class4">
  <li>
    <div class="other-element"></div>
  </li>
</ul>

    $('.options-container').each(function(i){ //holds multiple classes

            var classList = $('ul', this).attr('class').split(/\s+/),
            var myClasses = ['class1', 'class2', 'class3']; 
            //what classes to look for and match.    

         $.each(classList, function(index, item) {

           if (item === myClasses.index()) {
             $(.other-element).addClass(item); // append matched classes
           }

         });

    });

I'm not sure what I am doing with index..

Aucun commentaire:

Enregistrer un commentaire