Solved

jQuery $.each not working for array

Posted on 2014-02-05
2
455 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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jQuery Conflict 27 46
Java Server Faces parameter pass? 6 18
angularjs controller not found 1 16
ajax form sumbit 3 21
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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…
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)

762 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

12 Experts available now in Live!

Get 1:1 Help Now