I am using a .each loop to iterate over some div elements and grab the values of some input boxes contained within them. Each div represents a row and has an Id to signify this i.e. plot1, plot2.... I want to store all this data in an array but I'm not sure how to structure it. To give you an example of what I mean here is some code.
There are two divs that represent rows and each row has two input fields.
<div class="coords">
<div id="plot1" class="plotrow">
<div class="lat">
<input type="text" id="plot1_lat" value="1234" />
</div>
<div class="long">
<input type="text" id="plot1_long" value="4567" />
</div>
</div>
<div id="plot2" class="plotrow">
<div class="lat">
<input type="text" id="plot2_lat" value="6984" />
</div>
<div class="long">
<input type="text" id="plot2_long" value="2348" />
</div>
</div>
<button type="button" id="go">Go!</button>
</div>
Using jQuery I want to iterate over these divs and store the data in an array using the following format.
{ "<value from div id>": [{
"lat" : <<value from input>>
}, {
"long" : <<value from input>>
}
]};
Here is my function which is triggered when you click on a buttonw ith an id of go
$('#go').on('click', function () {
var object = {};
var array = [];
$('.coords input:text').each(function () {
var someArray = {
"plot": [{
"lat": this.value
}, {
"long": this.value
}]
};
array.push(someArray);
});
});
I'm a little confused over the array construction so I've put some dummy values in place but the "plot" should be the id from the div row and the lat and long should be the values of the two inputs within each div row.
Additionally I have found that because it's sitting within a .each loop it is create four arrays (one for each input) instead of one array with all the inputs organised by div id.
I hope that makes sense, can anyone offer some advice?