/    Sign up×
Community /Pin to ProfileBookmark

Get data-boxId value of all children

I’m trying to get the data-boxId attribute value in order of all items inside a continer…

I have this but it’s not working? can anyone help?

“`
function getItems(){
var ancestor = document.getElementById(‘container’),
descendents = ancestor.getElementsByTagName(‘div’);
for (i = 0; i < descendents.length; ++i) {
console.log(descendents[i]);
console.log(descendents[i].getAttribute(data-boxId));
}
}
“`

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 15.2022 — The quotes around `data-boxId</C> are missing. This works:<br/>
<C>
console.log(descendents[i].getAttribute('data-boxId'));</C>

There are two alternatives for your code:
<LIST><LI>- <C>
querySelectorAll</C> can be used to get any nested elements.</LI>
<LI>- <C>
dataset</C> can be used to access data attributes.</LI></LIST>

<CODE>
`<i>
</i> function getItems() {
const descendents = document.querySelectorAll('#container div');
for (let i = 0; i &lt; descendents.length; ++i) {
console.log(descendents[i]);
console.log(descendents[i].dataset.boxid);
}
}<i>
</i>
``
Copy linkTweet thisAlerts:
@interiorsapphiresJan 15.2022 — Interior Sapphire is an experienced interior designer with perfect contemporary taste. We are leading the best interior designer and Architect [Interior Design Services in Gorakhpur](https://bit.ly/3HYKhe2), Lucknow, Nepal, kanpur, varanasi, prayagraj, gwalior, Azamgarh, bagaha, jalalpur, Mubarakpur, Gopalganj, Uttar Pradesh.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...