Retrieve all image IDs based on their class name

My goal is to utilize JavaScript in order to retrieve the ID of each image on a webpage that is associated with the CSS class 'asset', and then store these IDs in a new array.

While I am able to collect all the images as shown below, I now need to extract their IDs into a separate array.

var image_ids = document.getElementsByClassName("asset");

Answer №1

What do you think of using Array.from in this scenario?

const ids = Array.from(
  document.getElementsByClassName("asset"),
  ({ id }) => id
);

Answer №2

Utilize the spread operator with getElementsByClassName, as it returns an iterable:

[...document.getElementsByClassName('content')].map(({name}) => name);

Answer №3

After much trial and error, I found a solution that specifically targeted IE9:

    let imageClassList = document.getElementsByClassName("asset");
    let imageIdList = [];
    
    for (let i = 0; i < imageClassList.length; i++) {
        let el = imageClassList[i];
        if (el.id) { imageIdList.push(el.id); }
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to specifically load a script for Firefox browsers

How can I load a script file specifically for FireFox? For example: <script src="js/script.js"></script> <script src="js/scriptFF.js"></script> - is this only for Firefox?? UPDATE This is how I did it: <script> if($. ...

What is the process of changing the name of an object's key in JavaScript/Angular?

In my possession is an established entity, this.data = { "part": "aircraft", "subid": "wing", "information.data.keyword": "test", "fuel.keyword": "lt(6)" } My objective is to scrutinize each key and if the key includes .keyword, then eliminat ...

Encountering an issue with Sails.js while attempting to connect to a cloud-based

When using cloud MongoDB with a sails adapter, I encountered an error while running the app. Can someone assist me in resolving this issue? default: { adapter: 'sails-mongo', url: 'mongodb://USERNAME:<a href="/cdn-cgi/l/email-protec ...

A guide to using universal-cookie to set cookies in getServerSideProps of NextJS

I am attempting to set a cookie using universal-cookie in the getGetServerSideProps function. import { NextPageContext } from 'next'; import Cookies from 'universal-cookie'; export const getGetServerSideProps = () => async ({ ...

What could be causing this JavaScript if statement to consistently evaluate to true?

I'm facing an issue where I want to run a specific block of code when a div is clicked for the first time, and then another block when it's clicked for the second time. The problem is that even though my alert shows the variable being updated wit ...

Placing a list item at the beginning of an unordered list in EJS with MongoDB and Node.js using Express

What I've done: I already have knowledge on how to add an LI to UL, but it always goes to the bottom. What I'm trying to achieve: Add an LI to the top so that when my div.todos-wrapper (which has y-oveflow: hidden) hides overflow, the todos you a ...

The React JSX error you encountered is due to the missing return value at the end of the arrow function

After implementing my code, I noticed the following: books.map(({ subjects, formats, title, authors, bookshelves }, index) => { subjects = subjects.join().toLowerCase(); author = authors.map(({ name }) => name).join() ...

What is the proper way to incorporate a ref within a class component?

I am encountering an issue with my class component. I'm wondering if there is a comparable feature to useRef() in class components? Despite several attempts at researching, I have yet to find a solution. ...

Creating a CSV download feature with ReactJS is simple and incredibly useful. Enable users

Despite searching through various posts on this topic, I have yet to find a solution that addresses my specific issue. I've experimented with different libraries and combinations of them in an attempt to achieve the desired outcome, but so far, I have ...

Sorting an array of Material-UI's <TableRow> alphabetically using ReactJS and Material-UI. How to do it!

I am currently utilizing Material-UI's <Table> and <TableRow> components by rendering an array of <TableRow>s using the .map() method. Each <TableRow> contains a <TableRowColumn> representing a first name, for example: &l ...

Using ng-src and ngFor in Angular applications

Imagine taking photos based on an ID and storing them in an array like this: data.attributes.photos = [ "12.jpg", "12_1.jpg", "12_2.jpg" , 12_3.jpg, ... ] Then, using ngFor to display them: <ngb-carousel> <ng-template ngbSlide *ngFor="l ...

Typescript struggling to load the hefty json file

Currently, I am attempting to load a JSON file within my program. Here's the code snippet that I have used: seed.d.ts: declare module "*.json" { const value: any; export default value; } dataset.ts: import * as data from "./my.json" ...

Adding a character to an AngularJS textbox

I am attempting to add the "|" Pipe symbol to a textbox when a button is clicked, using this function. $scope.appendPipe = function(){ var $textBox = $( '#synonyms' ); $textBox.val($textBox.val()+'|'); //textBox ...

Passing Selected Table Row Model Data to Backend in Angular 7

My goal is to send the selected data in a table row, which I select through a checkbox, to the server. However, I'm unsure about how to handle this via a service call. While I have the basic structure in place, I need assistance with sending the items ...

Adjust the size of a stacked object on top of another object in real-time

Currently, I am working on a project using three.js where users have the ability to modify the dimensions of a 3D model dynamically. The issue I'm encountering is similar to a problem I previously posted about stacking cubes together, which you can fi ...

Determine the present height of the current class and substitute it with another class that has the same

My wordpress blog theme has an ajax pagination feature that works well, except for the fact that when a user clicks on the next page link, the entire posts area disappears while the new content is loading. I would like to maintain the same container dimens ...

Tips for configuring a function to only be called once, even when the page is reloaded

I'm currently facing an issue with making a Post request upon component Mount. Every time the user reloads the page or there's a change in state, the function gets called again due to the useEffect hook, resulting in multiple requests being sent. ...

Include a new row in the form that contains textareas using PHP

I'm trying to add a new row to my form, but I'm facing challenges. When I click the add button, nothing happens. If I change the tag to , then I am able to add a row, but it looks messy and doesn't seem correct to me. Here is my JavaScript ...

When a JavaScript/jQuery open window popup triggers the onunload event after the about:blank page has been

Imagine you have a button that triggers a popup using window.open(): <button id = "my-button">Open window</button>​ You also want to detect when this popup window closes. To achieve this, you can use the following code: $('#my-button& ...

Is it possible to duplicate content from one div to another?

With over 90 divs, each containing an image or icon, I am looking for a way to display the clicked image in another div. I am not very familiar with JS yet, so I would appreciate a simple example using HTML5, Ajax, and js. EDIT: Just to clarify, the desti ...