cxwilson
cxwilson

Reputation: 107

How to get jquery variable childeren?

Hi I have a method that accepts an id as a parameter for my function:

function exportDetails($div) {
    $($div).each(function () {
        alert(this.id);
});

I need to get all the table's id's that are within this div, something similar to this:

function exportDetails($div) {
    $($div > table).each(function () {
        alert(this.id);
});

Here is how I am calling the function and each of the itemDetails have dynamically generated tables and I need to get their id's since they are all unique:

exportDetails.apply(this, [$('#itemDetails')]);

Thanks!

Upvotes: 0

Views: 33

Answers (2)

Alnitak
Alnitak

Reputation: 339786

Something like this should work:

function exportDetails($div) {
    return [].map.call($div.children('table'), function(table) {
        return table.id;
    });
}

i.e. just return an array containing the .id of every table element found as an immediate descendant of $div.

Upvotes: 3

andre mcgruder
andre mcgruder

Reputation: 1520

You code is not formatted correctly. Also, I don't know if you are feeding this function a group of tables that are inside of your $div parameter. Here are some options.

// this works if you pass in an element with nested table that have ids. 

function exportTableToCSV($div) {
    $($div+ ' > table').each(function () {
        alert( $(this).attr('id') );
    });
}

// this works if you want to get all the HTML elements with an id or if you want to get all the ids of elements which also have the same class assigned to them.

function exportTableToCSV($div) {
    $($div).each(function () {
        alert( $(this).attr('id') );
    });
}

exportTableToCSV('div');

// or

exportTableToCSV('.yourClass');

Upvotes: 0

Related Questions