deviloper
deviloper

Reputation: 7240

Reading table content of an external HTML file using jQuery

I have an external HTML file "mainstatus.htm" containing some tables. I load the file content into a div with the id "result" using the jQuery .load() function. Now I want see if some TDs in the second table inside the main table is holding a specific data. But it seems that I am not able to access the content of the loaded file using the way I have done bellow.

$('#result').load('http://localhost/mainstatus.htm', function() {
});
alert($("#result table:nth-child(2) tr:nth-child(1) td:nth-child(2)").val());

This alerts "Undifined"! Whcat point am I missing or what is even the correct approch?

Upvotes: 0

Views: 716

Answers (1)

Adil Shaikh
Adil Shaikh

Reputation: 44740

You need to put your alert inside your callback

$('#result').load('http://localhost/mainstatus.htm', function() {
  alert($("#result table:nth-child(2) tr:nth-child(1) td:nth-child(2)").text());
});

As load is asynchronous, your alert was executed before your data is even loaded,You need to wait for the data to be loaded before you can use it,that is why a callback function is there.

One more thing : You were using .val() to get content of td, you need to use .text() or .html()

Upvotes: 2

Related Questions