I'm having some trouble creating a multidimensional array where im putting some counties and places that are in the county.
Example of what im trying to do:
[
'HEDMARK' => [
1 => 'ELVERUM',
2 => 'HAMAR
],
'OSLO' => [
1 => 'OSLO'
]
]
The code im using is this:
var $query_place = [];
// Get each checked county
$.each($('.county input:checkbox:checked'), function () {
$query_place.push({
county: $(this).val(),
postal: []
});
});
// Get each checked postal
$.each($('.postal input:checkbox:checked'), function() {
$query_place[$(this).attr('data-county')].postal.push();
});
The error im getting in the console is this:
TypeError: $query_place[$(...).attr(...)] is undefined
Is there something im forgetting here? Or have i just done this wrong?
asked Sep 21, 2015 at 9:43
Kaizokupuffball
2,8438 gold badges44 silver badges64 bronze badges
2 Answers 2
You have to implement your code like this
var $query_place = [];
// Get each checked county
$.each($('input:checkbox.county:checked'), function () {
$query_place.push({
county: $(this).val(),
postal: setPostal($(this).val())
});
});
function setPostal(county)
{
var postal = [];
// Get each checked postal
$.each($('input:checkbox.postal:checked'), function () {
if ($(this).attr('data-county') == county)
postal.push($(this).val());
});
return postal;
}
answered Sep 21, 2015 at 11:08
Gagan Jaura
7091 gold badge5 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
you have to parse the $query_place array object before pushing any values into it.
$.each($('.county input:checkbox:checked'), function () {
var values={};
values.county= $(this).val();
values.postal=[];
var a=JSON.parse($query_place);
a.push(value);
})
answered Sep 21, 2015 at 9:45
kavinhuh
7391 gold badge9 silver badges30 bronze badges
Comments
Explore related questions
See similar questions with these tags.
lang-js
$().data( attributeName )better, and mind thecountytypo perhaps ^^$().datainstead, but I get the same error. I tried to switch the the inside of$.eachpostal line toalert($(this).val());but it doesn't alert anything, so i think i can't use $(this) for some reason.$query_place.county[$(this).attr('data-county')].postal.push($(this).val());