Atom Vayalinkal
Atom Vayalinkal

Reputation: 2702

sort checkbox values in javascript

I need to have all the values of checked checkboxes sorted with commas in javascript, so I can send the to a form sending php script.

The javascript:

var services = [];
$('#field-services:checked').each(function() {
services.push($(this).val());
});

$.post(rootUrl+'/wp-admin/admin-ajax.php', { action:"two2_send_contact_form", services:services }

I have the checkboxes inside a div with the id field-services the php that send the email

$services = $_POST["services"];
$subject = "BLAH BLAH";
    $body = "Services: $services, \n\n$message";

Upvotes: 0

Views: 619

Answers (1)

elclanrs
elclanrs

Reputation: 94131

$('#field-services:checked') of course won't work, because id must be unique so there must be only one #field-services checked. You probably want to do:

var services = []
$('#field-services input:checked').each(function(){
  services.push(this.value)
})

Upvotes: 2

Related Questions