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

  • Home
  • SEARCH
  • 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 8339773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:58:26+00:00 2026-06-09T04:58:26+00:00

I have the below checkboxes and I need to get them as an array

  • 0

I have the below checkboxes and I need to get them as an array values.

<input type="checkbox" id="contact_id" value="4" />
<input type="checkbox" id="contact_id" value="3" />
<input type="checkbox" id="contact_id" value="1" />
<input type="checkbox" id="contact_id" value="5" />

I need to pass them to one ajax request as array as below:

xmlHttp.open("POST","?action=contact&contact_id=" +contacts,true);

I am using this function to get the values but not able to pass them to the function as array, the passed like this 4,3,1,5. I need them to be passed like this

contact_id[]=4&contact_id[]=3&contact_id[]=1&contact_id[]=5

I have done this as follows

function getContacts(){
            var contacts = document.myform.contact_id, ids = [];
            for (var i = 0; i < contacts.length; i += 1){
                if (contacts[i].checked)
                     ids.push(contacts[i].value);
            }
            return ids;
        }
  • 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-06-09T04:58:27+00:00Added an answer on June 9, 2026 at 4:58 am

    http://jsfiddle.net/xQezt/

    Does this fiddle do what you want? The serialization is naive, but you could find a proper way to do that exact thing elsewhere or by using a framework like Zepto, jQuery or YUI.

    First I made a way to “submit” the data. The output goes to the console, so open your firebug. Could go anywhere, though.

    //submit event registration
    submitButton.onclick = function () {
        var contactArray = inputsToArray(contacts.children);
        var data = serializeArray(contactArray, 'contact_id[]');
        console.log(data);
    }
    

    Then I made your method “getContacts” more generic:

    function inputsToArray (inputs) {
        var arr = [];
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i].checked)
                arr.push(inputs[i].value);
        }
        return arr;
    }
    

    Here is the naive serialization function. I do not expect this to work well in all cases, so you should do some research in where to get a good serialization algo from:

    function serializeArray (array, name) {
        var serialized = '';
        for(var i = 0, j = array.length; i < j; i++) {
            if(i>0) serialized += '&';
            serialized += name + '=' + array[i];
        }
        return serialized;
    }
    

    I also slightly modified your HTML:

    <div id="contacts">
    <input type="checkbox" value="4" />
    <input type="checkbox" value="3" />
    <input type="checkbox" value="1" />
    <input type="checkbox" value="5" />
    </div>
    
    <button id="submit">Submit</button>
    

    Which let me query the DOM like this:

    var d=document;
    var submitButton = d.getElementById('submit');
    var contacts = d.getElementById('contacts');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
I have to select checkboxes besed on group column values. If you see below
I have below code in html <div id=divTest> Hello <input type=text id=txtID/> <input type=text
I have a problem to manipulate checkbox values. The ‘change’ event on checkboxes returns
I have dynamic number of checkboxes in jsp page as given below. <s:iterator value=details
I have 10 divs with forms inside of them and I need to get
If I have Checkboxes in page with IDs like below a_chk1, a_chk2, a_chk3, b_chk1,
I have added checkbox in display table using decorator class as shown in below
I have a code snippet as below <CheckBox Name=cb Margin=1,2,1,0 IsChecked={Binding Path=IsManager} IsEnabled=True/> Consider
I am going nuts here, I have an array of checkboxes from a form

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.