Solved

jQuery $.each not working for array

Posted on 2014-02-05
2
458 Views
Last Modified: 2014-02-06
Hi,

I have this bit of js code that grabs all the keys and values for a form:

    $.fn.calculate_selections.get_all_selections = function() {
        var inputData = new Array();

        $("#get_all_selections").find('input[type="text"]').each(
            function (unusedIndex, child) {
                inputData[child.name] = child.value;
            });

        $("#get_all_selections").find('input[type="hidden"]').each(
            function (unusedIndex, child) {
                inputData[child.name] = child.value;
            });

        $("#get_all_selections").find('input[type="radio"]:checked').each(
            function (unusedIndex, child) {
                inputData[child.name] = child.value;
            });

        $("#get_all_selections").find('select option:selected').each(
            function (unusedIndex, child) {
                var oClosest = $(this).closest('select');
                var sName = oClosest.attr('name');
                inputData[sName] = child.value;
            });

        $("#get_all_selections").find('textarea').each(
            function (unusedIndex, child) {
                inputData[child.name] = child.value;
            });
        return inputData;

    }

Open in new window


I return it from a plugin:
var aInputDataVals = $('').calculate_selections.get_all_selections();

Open in new window


But this part doesn't work:
    $.each(aInputDataVals, function (key, value) {
        calculate_final_amount(key, value);
    });

Open in new window


What am I doing wrong? I'm trying to debug in Chrome and it's not stepping into the $.each(). I can see aInputDataVals is an array of keys and values.

aInputDataVals['test'] = 1;
0
Comment
Question by:Victor Kimura
2 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
ID: 39837825
hi,
There may be issue with your creating array logic.

Please find below code to repeat among array items and create/update logic for creating array:

$(document).ready(function(){
    var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };    
    var arr = [ "one", "two", "three", "four", "five" ];
    alert("obj items are: ");
    $.each(obj, function(index, value) {
        alert(index + ": " + value);
    });
    
    alert("arr items are: ");
    $.each(arr, function(index, value) {
        alert(index + ": " + value);
    });
});

Open in new window


hope this helps you.
0
 

Author Comment

by:Victor Kimura
ID: 39839409
Hi Mrunai,

Yes, if I use the array like in your example var arr then I found the indexes are 0, 1, 2, etc.

but the function I have must use var obj = {} like in your example since it's actually an object not an array that is created I think. Anyways, changing the line from:

var inputData = new Array();

to:

var inputData = {};

does the trick.

Thank you! =)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now