Krishna
Krishna

Reputation: 1362

Get the exact child Table using Jquery

I have a situation here... I have two div's content1 and content2,each div has a table also... so i get each table like this

$("#content1 table") 
or 
$("#content2 table")

but I want Like this

var x="#content1" or "#content2"

then something like this

$(x "table")

i.e pass the id to a variable and get the table... any solution?

Upvotes: 1

Views: 81

Answers (7)

Edward
Edward

Reputation: 1914

For more simplicity:

$(x + " table");

Upvotes: 1

Roopesh90
Roopesh90

Reputation: 80

The answer lies in the example you gave itself. Just have to modify it slightly

var x="#content1"; //whatever the name of the parent is
$resulting_table = $(x+" table")

Hope that helps.

Upvotes: 2

Balachandran
Balachandran

Reputation: 9637

Use find to get the table

 $(x).find("table");

Upvotes: 2

Ramniwas chhimpa
Ramniwas chhimpa

Reputation: 205

    var x= document.getElementById('content1');
     $(x).find("table");

try this one

Upvotes: 4

tuan huynh
tuan huynh

Reputation: 727

use find function $(x).find("table")

Upvotes: 2

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try to use the context i.e] $('selector',context),

var x= "#content1";
var table = $('table', x);

And the above code is very much similar to,

var x= "#content1";
var table = $(x).find("table");

Upvotes: 2

Adil
Adil

Reputation: 148110

You can combine selector using comma by Multiple Selector (“selector1, selector2, selectorN”)

$("#content1 table, #content2 table") 

Alternatively you can assign common class to table with #content1 and #content2

$('.common-table-class')

Upvotes: 2

Related Questions