Solved

jQuery $.each not working for array

Posted on 2014-02-05
2
460 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP AJAX JSON 2 61
JqueryUI sortable append variable to post 1 38
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 33
JavaScript let vs var 5 25
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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)

770 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