Get 2 "arrays" in one line?

2

For example, how do you get the 2 in a single line?

Example:

document.getElementsByClassName("teste")[0].removeAttribute("disabled");

I want to get 0 and 1

You can get the 2 together, or I'll have to create two lines like this;

document.getElementsByClassName("teste")[0].removeAttribute("disabled");

document.getElementsByClassName("teste")[1].removeAttribute("disabled");

?

    
asked by anonymous 11.07.2017 / 17:00

2 answers

3

With ES6 you can do this in one line like this:

[...document.getElementsByClassName("teste")].forEach(el => el.removeAttribute("disabled"));

But with "old" JavaScript has to be with a loop:

var els = document.getElementsByClassName("teste");
for (var i = 0; i < els.length; i++){
    els[i].removeAttribute("disabled")
}
    
11.07.2017 / 17:16
0

This should work:

Array.from(document.getElementsByClassName("teste")).map((ele, idx)=> idx === 0 || idx ===1 ?  ele.removeAttribute("disabled") : ele);

EDIT:

The getElementsByClassName method does not return a array . So you need to convert it to array for this solution to work. I edited the answer by converting it to array .

    
11.07.2017 / 17:06