0

I have this structure:

<div id="fields" class="grid">
    <div>...<div>
    <div id="some_id_1" class="itemSortable">
        <input ...>
        <input ...>
        <div>
            <input id="selected" name="selected" type="checkbox" value="true">
            <input name="selected" type="hidden" value="false">
            ...
        </div>
    <div id="some_id_2" class="itemSortable">
        <input ...>
        <input ...>
        <div>
            <input id="selected" name="selected" type="checkbox" value="true">
            <input name="selected" type="hidden" value="false">
            ...
        </div>
     ...
    <div id="some_id_n" class="itemSortable">
        <input ...>
        <input ...>
        <div>
            <input id="selected" name="selected" type="checkbox" value="true">
            <input name="selected" type="hidden" value="false">
            ...
        </div>
</div>

I have no experience with jQuery so I am stuck. Can somebody help with jQuery script to get an array of all ids (Eg. some_id_x) for selected checkboxes?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
user1734523
  • 35
  • 1
  • 4

5 Answers5

2

To get an array of the ids of the closest parent .itemSortable of the selected checkboxes, try this:

var idArray = $('#fields').find(':checked').map(function() {
    return $(this).closest('.itemSortable').prop('id');
}).get();

You seem to have a lot of duplicate id attributes though which means your HTML is invalid and will lead to problems in your code.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    I think he wants to know the parent element's ID, so return `$(this).parents("div.itemSortable").attr("id");` should do the trick. – Olly Hodgson Dec 18 '12 at 13:57
1

First you get the checkboxes themselves:

$(':input:checked');

Then you find their respective parents:

$(':input:checked').parents('.itemSortable');

To get each id, use .each() to iterate over the resulting list:

var ids = [];
$(':input:checked').parents('.itemSortable').each(function() {
    ids.push(this.id);
});
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

css id's are meant to only be on 1 element on a page, use classes.

$('.someClass:checked').serialize()

Adam Merrifield
  • 10,307
  • 4
  • 41
  • 58
0

use :checked Selector

Details on http://api.jquery.com/selected-selector/

Example:

$(function()
{
    var idArray = new Array(); $('#fields').find(':checked').each(function() 
   {
       idArray.push(this.id);
   });

   console.log(idArray);
});
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64
0

in order to retrieve the checked inputs of type check (check-boxes), use the ":checked" selector. this example can explain my point:

Html:

<div id="fields" class="grid">
        <div id="some_id_1" class="itemSortable">
            <div>
                <input id="selected" name="selected" type="checkbox" value="true" checked="checked"/>
            </div>
        </div>
        <div id="some_id_2" class="itemSortable">
            <div>
                <input id="selected" name="selected" type="checkbox" value="true" checked="checked"/>
            </div>
         </div>
        <div id="some_id_n" class="itemSortable">
            <div>
                <input id="selected" name="selected" type="checkbox" value="true"/>
            </div>
        </div>
    </div>

javascript/JQuery:

$(document).ready(function () {
            alert($("#fields :checked").length);
        });

this example will alert number two; the count of checked check-boxes in element with id "fields" .

PS:

  • I hope this is what you have requested.
  • to make input of type checkbox is checked use the attribute checked="checked"
Mr.H
  • 61
  • 8