0

I have a contact list as below:

<div class="contacts_list_data_contacts" id="contactlist">
<div class="contacts_checkbox_01"><input name="contact_id[]" id="contact_id" type="checkbox"  value="28383" style="margin-top:0px ; margin-left:-3px;" /></div>
<div class="contacts_firstname_01_contacts">Michael</div>                                 
<div class="ncontacts_mobile_nmbr_01">+44515665544</div>
</div>

<div class="contacts_list_data_contacts" id="contactlist">
<div class="contacts_checkbox_01"><input name="contact_id[]" id="contact_id" type="checkbox"  value="28383" style="margin-top:0px ; margin-left:-3px;" /></div>
<div class="contacts_firstname_01_contacts">Katherine</div>                                 
<div class="ncontacts_mobile_nmbr_01">+30589746621</div>
</div>

It looks like that:

Firstname     MobileNumber
===========================
Michael       +44515665544
Katherine     +30589746621          

I am using ajax request to delete a contact

contact_id=document.getElementById('contact_id_ajax').value;
    xmlHttp.open("POST","?action=ajaxcontact&todo=DeleteContact&contact_id=" +contact_id ,true);

Using the below function I am able to get ONLY one contact at once by put a checkbox. I don't know how to pass all of the contacts when I select all the contacts and pass them to the ajax request.

<Script>
var field;
        function get_contact(field)
        {
            for (i = 0; i < field.length; i++)

            if(field[i].checked)
             document.getElementById("contact_id_ajax").value=field[i].value;
        }
</Script>

Thank you,

4
  • you want all the checked check boxes and then the contacts for them? Commented Jul 29, 2012 at 7:13
  • Yes, and pass the array to the ajax request Commented Jul 29, 2012 at 7:14
  • What is the value of the check box? Commented Jul 29, 2012 at 7:17
  • So you want an array of contact ids then? Commented Jul 29, 2012 at 7:18

1 Answer 1

1

First things, first. You have a lot places where you use the same IDs for elements. This is very bad practice indeed. IF you want a way to identify a collection of similar items, use classes (you already have - just get rid of the ids or get unique ones).

Now, if you add a class to your checkboxes like so -

<input name="contact_id[]" class="contact_id" type="checkbox"  value="28383" style="margin-top:0px ; margin-left:-3px;" />

Then in your getContacts function, you can skip the argument and go with -

function getContacts(){
    var contacts = document.getElementByClassName('contact_id'), ids = [];
    for (var i = 0; i < contacts.length; i += 1){
        if (contacts[i].checked)
             ids.push(contacts[i].value);
    }
    return ids;
}

Now, when you want the contacts list -

var contacts = getContacts(); //array of contact ids
//This array can now be used in your ajax request
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer, and how I pass the array to the Ajax request?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.