Search code examples
javascriptmatchnodelistqueryselector

Can I match forEach variable within document.querySelectorAll function


I am trying to refactor my document.querySelectorAll code combining several classes/types together in one function rather than have several functions perfoming the same task but for a different class/type.

Sample

document.querySelectorAll("button[name='checkout']","input[name='checkout']");

x.forEach(el => el.setAttribute("disabled","disabled"))

This works to match on the first match (the button element), but it does not match for every class after that (ie the input element).

How do I adjust my code to get my desired result of disabling both the button and the input?


Solution

  • querySelectorAll takes only a single argument, not two. You can however pass a single selector list that matches multiple elements:

    document.querySelectorAll("button[name='checkout'], input[name='checkout']");
    //                                                ^^