/    Sign up×
Community /Pin to ProfileBookmark

How do I save html elements as an array with objects?

hello. I am trying to save some elements as an array with objects but I cant get it to work.

This is what I have done so far

[CODE]
function getElements() {
var test= document.getElementsByClassName(“testclass”);
var len = test.length;
var arr= [];

for(var i= 0; i<len; i++){
arr.push({
id: test[i].id,
value: test[i].value,
title: test[i].title
},);
}
}
[/CODE]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rootApr 25.2018 — If you want a straight copy, then use the array copy method, you should be able to make a copy of that to a new array by using a process like

newArray = Array.prototype.slice.call( the_array_to_be_copied ); so something like
newArray = Array.prototype.slice.call( document.getElementsByClassName("testclass") ); should work.
×

Success!

Help @bbq1 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 5.5,
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,
)...