Home > Back-end >  Address attributes of HTML tags by CSS selector
Address attributes of HTML tags by CSS selector

Time:09-26

I'm loading the name and id attributes of a <form> called contactForm to a javascript array like this:

const flds=[];
contactForm.querySelectorAll("input").
    forEach( value => { 
        flds.push([value.id, value.name])
    });
;
console.log(flds);

Can I do that directly without a loop? Something like:

const flds=contactForm.querySelectorAll("input [name,id]")

CodePudding user response:

I think the best you can do is a map function.

const flds = [...contactForm.querySelectorAll("input")].map(el => [el.id, el.name])
  • Related