There are elements:
<div class="hello 1"></div>
<div class="hello a"></div>

It is necessary to place in the attribute the second class of each element that has a class hello.I can not understand how to write a loop iteration correctly.

2 Answers 2

var hello=document.querySelectorAll(".hello");

for(var i=0;i<hello.length;i ++) {
	hello[i] .setAttribute("classAttr", hello[i] .classList[1]);

$(".hello").attr("classAttr", function() {
	return this.classList[1];
I can’t understand how to write a iteration loop correctly.

The loop is not needed, the attr method can take a function and apply it to each element of the set:

$('.Hello').Attr('data-class', function() {
  return[...this.classList] .filter(n=>n! =='hello');

True, there is one ambiguous point - is it guaranteed that there will be only two classes? What if there are more?
  • I think these people ES5/ES6 will be difficult to understand) – Forbidden64 Jan 8 '19 at 21:54
  • What if there are more of them?

    Address simply the second)))
    – Forbidden64 Jan 8 '19 at 21:58
  • The66, there can be as many classes as you like.
    Forbidden64, I don’t mind using a new one, but since js is non-core for me, I haven’t studied new versions.
    – Dr.100 Jan 8 '19 at 21:59
  • Dr.100,

    classes can be as many as you like

    Copy everything except"hello"? If so, how should they be combined? If not, how to understand which one should be copied?
    – The66 Jan 8 '19 at 22:00
  • Forbidden64,

    Addressing simply the second

    Unacceptable. Who guarantees that the desired class will be exactly the second?
    – The66 Jan 8 '19 at 22:01
  • The66, Yes, I am kidding) here the task itself is so-so to take only the second class) many nuances. – Forbidden64 Jan 8 '19 at 22:04
  • Although the author gives a clear task)
    It is necessary to place the second class of each element in the attribute
    – Forbidden64 Jan 8 '19 at 22:05
  • The66, Forbidden64, but is there any guarantee that this second class in the array will not be hello? those. does the order of the elements in the classlist array correspond to the order in which the classes are written in the class attribute itself in the tag? – Busy17 Jan 8 '19 at 22:26
  • Busy17, and how do we relate to its project structure?))) – Forbidden64 Jan 8 '19 at 22:33