Hi there i would like to select forexample second table in html document. I was trying to do so by table:nth-child(2) or table:eq(1)
but doesnt work. I will appreciate your answers :)
Asked
Active
Viewed 7,366 times
2

GentSVK
- 324
- 1
- 3
- 12
-
`$("#container").find(selector).html();` and selector is parsed from xml document. Maybe there is a problem. – GentSVK Aug 12 '12 at 07:48
4 Answers
2
You should use the eq filter
$('table:eq(1)')
More on this topic in the doc http://api.jquery.com/eq-selector/. Remember always the $(document).ready(... call at the beginning of your definition.

Giova
- 1,879
- 1
- 19
- 27
-
-
@MarcelGentSimonis It's fixed: sorry I'm typing on my phone with some little errors.. – Giova Aug 12 '12 at 07:47
-
-
@MarcelGentSimonis is your script correctly loaded? is the event which execute your code triggered? there are errors in the JavaScript error console of the browser? Have you tried printing something with window.alert()? – Giova Aug 12 '12 at 08:01
-
im am using cross domain query using yql and loading results in #container div which is hidden after this im trying to select second table from within div #container. in console there are errors like some pictures were not found code 404 and thats all. But if im trying to select some div within #container it works but second table doesnt. – GentSVK Aug 12 '12 at 08:12
-
all your answers works i have problem somewhere else sry for disturbing you from sundays morning caffee :) – GentSVK Aug 12 '12 at 08:19
-
@MarcelGentSimonis no problem. take a look at this other question http://stackoverflow.com/questions/815074/jquery-doesnt-see-new-elements – Giova Aug 12 '12 at 08:21
2
This should also work selecting all the tables and then finding the right one using [] gives access to the js object and then you convert the js object back to a jQ object.
$($("table")[1])

Pablo Jomer
- 9,870
- 11
- 54
- 102
1
I would have also suggested:
$('table:nth-child(2)')
However if that isn't working, perhaps:
$('table').first().next('table')

Joel Friedlaender
- 2,191
- 1
- 18
- 24
-
1`next()` selects the next element in the DOM, it doesn't search for the next table, for example. – Ram Aug 12 '12 at 07:34
1
I think table.eq(1)
should $('table').eq(1)
. See here.
But, if your table
is a variable then it should be a jQuery object. For example:
var table = $('table'); // will returned all existing tables of document (jQuery object)
table.eq(1);
will work just fine. See here.

thecodeparadox
- 86,271
- 21
- 138
- 164