Solved

jQuery $.each not working for array

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
chrome and edge version of getElementById("p").attributes("title").value 2 36
ajax to record click 3 16
Html Newline 7 21
Wordpress Only run code if on a certain page 11 23
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…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)

828 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