Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  174] [ 6]  / answers: 1 / hits: 108607  / 11 Years ago, tue, november 5, 2013, 12:00:00

This is what I have so far and the shoe types are boots, wellingtons, leather, trainers (in that order)



I want to iterate through and assign the value so I haves something like



var shoeArray = { boots : '3', wellingtons: '0', leather : '1', trainers: '3'};


at the moment I just get an array of {3,0,1,3} which I can work with but it is not very helpful.



function shoe_types() {
var shoeArray = [];
$('[type=number]').each(function(){
$('span[data-field='+$(this).attr('id')+']').text($(this).val());
shoeArray.push ( parseInt($(this).val()) );
});
return shoeArray;
}

More From » jquery

 Answers
43

Check this function



function shoe_types() {
var shoeArray = {}; // note this
$('[type=number]').each(function(){
$('span[data-field='+$(this).attr('id')+']').text($(this).val());
shoeArray[$(this).attr('id')] = parseInt($(this).val()) ;
});
return shoeArray;

}


PS: Assuming $(this).attr('id') has all the shoe types


[#74499] Monday, November 4, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ashli

Total Points: 210
Total Questions: 94
Total Answers: 96

Location: Austria
Member since Sat, Jul 24, 2021
3 Years ago
;