Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:48:37+00:00 2026-05-22T16:48:37+00:00

Where I got stuck: In my spare time I work on a private website.

  • 0

Where I got stuck:

In my spare time I work on a private website. My self-teaching is really unstructured, and I’ve run up against a huge hole in my fundamentals.

I’m looking at a jQuery example from the jQuery API website, serializeArray, and I can’t wrap my head around the ShowValues function.

Here’s how it goes:

function showValues() {
  var fields = $(":input").serializeArray();
  $("#results").empty();
  jQuery.each(fields, function(i, field){
    $("#results").append(field.value + " ");
  });
}

$(":checkbox, :radio").click(showValues);
$("select").change(showValues);
showValues();

And I’m pretty sure I get what’s going on in everything except lines 4 and 5:

  jQuery.each(fields, function(i, field){
    $("#results").append(field.value + " ");

jQuery goes through each key in the fields array and puts what it finds through the generic function: function(i,field)

that function uses its two parameters to produce a string to append to #results.

My questions:

Why does that function need two parameters? The variable i seems to count up from zero, for each time the function is run. and if it’s taken out of the function, field.value returns undefined.

Since the values coming in to the two-parameter function are arranged into an array, the function has to…match the dimensions of the array?

Is i special, or could any spare variable be used?

And what’s happening with field.value? .value isn’t in the jQuery API, but I think it’s still plucking values from the second position in the fields array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:48:38+00:00Added an answer on May 22, 2026 at 4:48 pm

    Why does the function need two parameters?

    First of all you should read the documentation of .each. It tells you that it expects a function defined as such:

    callback(indexInArray, valueOfElement)
    

    That has been decided, so you have to abide to it (the reasoning is beyond this answer).

    If you define your callback as such

    function(indexInArray, valueOfElement, foo) { ... }
    

    and it is called as

    callback(indexInArray, valueOfElement)
    

    then foo will be undefined.

    If you define your callback as

    function(foo) { ... }
    

    it will still be called as

    callback(indexInArray, valueOfElement)
    

    and foo will contain the indexInArray – and 0.value will of course be undefined if you “leave out the i“.

    Can I use a spare variable?

    Yes you can. In most functional languages you will find _ used for parameter that you don’t care about (be careful if you use libraries like underscore.js though). Any other name can be used.

    $(...).each(function(_, elem) {
      ...
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've been working on a problem in my spare time and I'm stuck.
Got stuck here: http://jsfiddle.net/UFkg8/ Right now the animation is top-down. What do I need
I got stuck among the weared behaviour of browsers. Ihave a div which has
I got stuck again between browsers compatability issues, what I was trying todo is
I got stuck in this problem for an hour. I am thinking this is
I got stuck with this problem. I wrap two tables inside of a h:form.
I got stuck when chmod a file. At the bottom of this page ,about
I've got stuck in a problem with gflags when trying to find some memory
I'm following the Django tutorial and got stuck with an error at part 4
I am working on a problem and got stuck at a wall I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.