How to change the class or add?

There is such code
<div class="myClass">
<select>
 <option>Item 1</option>
 <option>Item 2</option>
</select>
</div>

Need if code is wrapped in a class myClass, then to add or replace a class for the select takest if < div class="myClass">, the select statement becomes < select class="myClassselect">, push in the right direction
August 23rd 19 at 11:08
3 answers
August 23rd 19 at 11:10
Solution
If we do not know the class in which is wrapped the select in advance, as follows: (for all selectors)
var selectList = document.getElementsByTagName('select');
for (var i = 0; i < selectList.length; i++) {
 var parentClass = selectList[i].parentNode.classList[0]
 selectList[i].classList.add(parentClass + "Select")
}

If we know and we need to check the parent class and add the select, like this:
var selectList = document.getElementsByTagName('select');
for (var i = 0; i < selectList.length; i++) {
 var parentClass = selectList[i].parentNode.classList.contains('myClass')
 if (parentClass) {
 selectList[i].classList.add("myClass" + "Select")
}

}
August 23rd 19 at 11:12
Solution
Array.from(document.querySelectorAll('select'), item => {
 if (item.parentNode && item.parentNode.classList.contains('myClass')) {
item.classList.add('myClassselect')
 } 
})
August 23rd 19 at 11:14
Thank you to all of the options work

Find more questions by tags JavaScript