I am in search of the most effective method for extracting multiple span tags from various spans and organizing them into an array or JSON object for future use. (I'm unsure whether an array or JSON is the optimal choice.)
HTML:
<span class="car" id=c1 data-make="volvo" data-year="2010">Car1<span>
<span class="car" id=c2 data-make="saab" data-year="1998">Car2<span>
JavaScript:
var cars = document.getElementsByClassName('car');
for(var i=0; i<cars.length; i++) {
<- what should be included here? ->
}
Currently, I have created three separate arrays for ID, make, and year, but it seems disorganized. I am struggling with how to create:
Array(
[0] => Array(
[id] => c1
[make] => volvo
[year] => 2010
)
[1] => Array(
[id] => c2
[make] => SAAB
[year] => 1998
)
);
Or a JSON object:
jsonString = [
{
"id": "c1",
"make": "volvo",
"year": "2010",
},
{
"id": "c2",
"make": "saab",
"year": "1998",
}
];
The purpose of this is simple. I intend to use this information to replace innerHTML like so:
document.getElementById(car[id]).innerHTML = car[make]
So, two questions: 1) What would be more suitable for this task - multi-dimensional array or JSON object? 2) What should be inserted in the section of my loop to store the data in that array or JSON?
Thank you - I am still gaining knowledge.