3

I have a for of statement in JS that returns HTML element ids but they are in multiple outputs. I would like to take these outputs and put them in an array.

For example the HTML is:

<div>
    <button id="savebtn-1"></button>
</div>
<div>
    <button id="savebtn-2"></button>
</div>

and my JS is:

const elements = document.querySelectorAll("button");
for (const element of elements) {
    let id = elements.getAttribute("id");
}

console.log(id) will show:

savebtn-1

savebtn-2

How can I get these outputs into an array?

Thanks for any attention you may give this question.

6
  • 3
    You already have the elements in the list, why to mess with the ids ..? Commented Sep 28, 2022 at 5:54
  • The buttons save a a text area value to the local storage. I have nine buttons and all nine are working to that effect. I t's just a stupid amount of redundancy that I have written for each button. I want to iteratively apply the event listener to each button. Commented Sep 28, 2022 at 6:18
  • I'm thinking that looping through an array will achieve this Commented Sep 28, 2022 at 6:19
  • 2
    Umm ... You can iterate the NodeList, and add event listeners. It sounds like you could make your code much simple by using event delegation. Ids are purposed to be used for elements not related to each other in any way, these savebuttons are more like a class of buttons. Commented Sep 28, 2022 at 6:23
  • 1
    Teemu, I really appreciate your time. You have given me much to chew on. I am a mental infant in this space but I am enjoying the growing pains. You rock my friend! Keep making the world a better place. Commented Sep 28, 2022 at 16:44

3 Answers 3

3

Make another array and store all the id's inside that array.

const resultArray = [];
const elements = document.querySelectorAll("button");

for (const element of elements) {
    const id = element.getAttribute("id");
    resultArray.push(id);
}
console.log(resultArray);
<div>
<button id = "savebtn-1">Button 1</button>
</div>
<div>
<button id = "savebtn-2">Button 2</button>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

You rock Mr. Mack! Super helpful
Thanks yogi. I had a supervisor tell me that I like to do things the hard way... yep. I appreciate you keeping to the context of the question.
1

Use Array.from() on the NodeList and a custom mapper to extract the IDs

const idArray = Array.from(
  document.querySelectorAll("button"),
  ({ id }) => id
);

console.log(idArray);
<div>
  <button id="savebtn-1">Button #1</button>
</div>
<div>
  <button id="savebtn-2">Button #2</button>
</div>

1 Comment

Thanks a bunch phil! I was so close to this solution earlier but somehow I was get nine single arrays like: ["savebtn-1"] ["savebtn-2"] and so on.
1

You can create an empty array and push ids into that array.

const elements = document.querySelectorAll("button");

const idArray = [];

for (const element of elements) {
    let id = elements.getAttribute("id");
    idArray.push(id);
}

console.log('idArray ', idArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.