views:

175

answers:

4

I am somewhat new to jQuery but have been having fun trying learn about all the cool tricks it can do. One of the current things I am trying to use is the load function to get a weather report from one the sites we use for daily reporting. The code I am using is

$(document).ready(function() {
$("#table").load("weather_url table");
});

This works just like I expected and returns all tables. The problem I am having is that none of the tables have an ID tag, so I get all the tables in the page. I need to get only one table that has the weather information in it. I am not sure how to target the one I need. Is this even possible without IDs? I have also tried using

$('table:last-child').show();

But it still shows all the tables.

Thanks for the help with this!

A: 

Use

$('#table:last-child').show();

I assumed that the visibility of all tables loaded are hidden and you just want to show the last table.

rahul
He already said that he tried to do it in this way, but I guess that all tables are visible.
Balon
Note the added `#` in the answer
Veger
A: 

there is a function in jquery that gives you the nth item

http://api.jquery.com/eq-selector/

joetsuihk
Winner winner, chicken dinner! The nth item was able pull out the exact table I needed!Thank you!
Jason
+2  A: 

You should be able to do it directly in the selector after the url. See "Loading page fragments" here: http://api.jquery.com/load/

$(document).ready(function() {
    $("#table").load("weather_url table:last");
});

You should also know that it is bad for performance if you load a complete page and only use a small part of it. Then it would be better to separate the last table to its own page, and include (preferably server side) that in the original page.

gregers
A: 

try

$(document).ready(function() {
    $("#table").load("weather_url table:last-child"); // get last
}); 

-- ADDED edited --

if you're having trouble with unstructured html source, you can also try:

  $("#table").load("weather_url table:contains('unique text for filter')"); 

this will get a table that contains 'unique text for filter'.

more of how to use :contains() here.

Reigel
@gregers: is there ':last' in jQuery? or you mean ':last-child'?
Reigel
this is money my friend! still not quite there but this did give me only the last table. Because the source I am grabbing is pretty much unstructured, there seems to be a lot of nested tables, so this gets me to the last table but there is apparently another nested table, which is the one I need. I have gone over the html almost line by line and can verify that this table is by itslef, just nested. the table returned has a nav bar on the left side in its own table and the weather on the right in its own table, both of these are nested in another table. Can dial this in any more? Thanks!
Jason