Creating an array from a collection of dataset Id values acquired from a div

The dataset Id is linked to the image. Suppose I have assigned different dataset Id values to a set of divs, how can I gather these values and create an array with them collectively?

I attempted to loop through them as they all have the same class name, but it only returns individual arrays instead of one combined array. Using array.from() won't work either because it will just break down the string into separate characters. Any suggestions on how I should proceed?

Answer №1

To start off, use the document.querySelectorAll method to target all elements with a specific class. After that, implement a mapping function to access the dataset.id property.

const result = Array.from(document.querySelectorAll('.b'), item => item.dataset.id);
console.log(result);
<div data-id="200" class="b"></div>
<div data-id="201" class="b"></div>
<div data-id="202" class="b"></div>

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

Generate items above the mesh

I'm currently working on a procedural terrain generation project. I have successfully generated terrain using Perlin noise, and now I want to add procedural grass generation with some specific constraints. My goal is to only generate grass within a c ...

Utilize JQuery's appendTo() method along with html() function for seamless content replacement

I have a question about appending and replacing div elements. I am currently using the following code: $('.toggle_questions').appendTo('#'+sectionId).show(); While this works perfectly to append my div with the class .toggle_questions ...

How can I import an excel spreadsheet using Javascript, or alternatively, convert an excel file to a text document

I have been updating my file upload page to handle both text and excel files. However, when trying to read excel files with my current code, I am getting strange output. It seems that my function, which was originally designed for text files, needs modific ...

Is there a way to inform jQuery to restrict all selectors I use to the current object I am manipulating?

Behold, I have created a revolutionary plugin that holds the power to transform the world http://jsfiddle.net/4phfC/1/: <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> <script ...

Ways to utilize this node.js module within a specific file

This is the third party node-js module I have created: var knox = require('knox') , Resource = require('deployd/lib/resource') , httpUtil = require('deployd/lib/util/http') , formidable = require('formidable') ...

jquery did not load properly in the Google Chrome extension

Why am I unable to use a jQuery script inside my "myscript.js" file in the following Google Chrome extension? Is jQuery not loaded within the "myscript.js" file? What changes need to be made in the manifest file to enable the usage of jQuery inside "myscri ...

Hunting for a vacant spot within an array

I've been working on a program that searches for an open slot in an array and then inserts a value into that slot. While I know how to do a linear search for a specific number within an array, I'm struggling with identifying empty spaces. Any sug ...

Locate a particular word in every element within an array range (angularjs)

I'm just getting started with learning angularjs and I would appreciate some kindness as a beginner. Currently, I am working on creating a poll app using angular. In my project, I have an array scope named items, which holds all the items to be adde ...

Is it necessary for an embedded YouTube video to automatically play with sound?

My current project involves developing a Web Application where the client has specified that videos must autoplay with sound when a user visits it. Here is a snippet of what I have implemented: HTML: <embed id="video1" src="" wmode="transparent" type= ...

Array failing to populate with accurate information

During the loop, I am populating an array: for k in one two three; do array+=( "$k" ) done echo $k[0] # Expecting to print 'one', but prints 'one[0]' echo $k[1] # Expecting to print 'two', but prints 'one[1]' W ...

Errors can occur in React/Axios when API calls are served over HTTP instead of HTTPS

I'm currently utilizing an API to retrieve movie data by making use of axios within my React application. This functionality is operating as intended in my local environment; however, when I deployed the app to github pages, it ceased to function prop ...

Vue.js video player component displays at a width of 100 pixels

I recently started implementing the vue-video-player library into my project. However, I'm facing an issue where the player renders at a width of only 100px. I'm not sure why this is happening or how to adjust it. After going through the documen ...

Top storage solution for ExpressJS in the world of NodeJS

Embarking on the journey of creating my first substantial NodeJS application. My primary focus is achieving top-notch performance as the project involves a large AJAX (AngularJS) interface with numerous requests from multiple users. Currently in the proce ...

How to rotate an object in Threejs using the mouse without having to click and drag?

I'm searching for a solution to rotate around an object in Threejs without the need to hold down the mouse button. A good example can be found on this website: which utilizes Threejs. Despite searching through forums and documentation, I have been un ...

Seeking help with executing JQuery Ajax functions within a foreach loop

Currently, I am engrossed in the study of programming and endeavoring to construct a website utilizing .Net Core. The predicament at hand pertains to my limited acquaintance with JavaScript while incorporating two JQuery/AJAX functions on my Index page - o ...

Learn how to use React.js props in your index.html file to

Issue: Unable to locate React props I am currently facing difficulties in accessing React props on the index.html page. This is the main page where I need to render meta tags properties that are fetched from the backend. I need to access my store data on ...

Menu/navigation bar designed with floating lines and an array of color options

I'm currently working on implementing this specific menu into my Wordpress site. My main concern is figuring out how to customize the hover effect for each navigation item. Currently, the float line changes to red (background-color:#800; height:2px;) ...

Tips for testing an API that relies on an external library such as "<script src="http://stripe[...]"

Currently, I am working on unit testing an API call to verify it has been executed with the correct properties. The API call is reliant on Stripe's external library that is connected to the window through index.html src="http://stripe[...]". However, ...

Discover the perfect method for combining two objects while updating any empty values with a new specified value. Furthermore, in the case where the new value is also

My task involves working with an array of objects where each time I select a value, it gets pushed into the array. My goal is to merge two objects that share the same key "code" and remove any empty values. (4) [{…}, {…}, {…}, {…}] 0: {code: "abc ...

What is the best way to extract a hashed password from my database using bcrypt encryption?

I am having trouble implementing bcrypt in my login post method. I have successfully stored encrypted passwords in the database during registration, but can't figure out how to compare them when users log in. Below is my register post method where th ...