1

I have a weird question. I want to create script in javascript (or PHP if I have to, I'm just more comfortable in javascript) that reads through a table until it finds specific text (know how to do that) and then returns the text three cells to the right.

To be specific, I want a script that finds a row for a specific printer on this site and returns the printer's status. I know how to select unique text, but not so much about nonunique text.

Salem
  • 1,122
  • 4
  • 19
  • 30
  • Sure, you could do that with jQuery/Javascript. I would start by looking at jQuery.get(), passing the URL of the link you provided. You can wrap the response with a DIV tag and manipulate it as you would another jQuery object. – Dan A. Jan 31 '12 at 03:19
  • All of the functions here seem to return null after attempting to filter the element out. – Salem Jan 31 '12 at 05:11
  • (Oh, and I'm running them on the actual page, not trying to load them yet). – Salem Jan 31 '12 at 05:20

3 Answers3

2
var printerName = 'yourname',
    status = $(':contains(' + printerName + ')').siblings().eq(2).text();
Eli
  • 17,397
  • 4
  • 36
  • 49
  • I think the [:contains](http://api.jquery.com/contains-selector/) selector will also match printers that have `printerName` as a **substring**, right? (which may or may not be a problem, and can even be desirable...) – mgibsonbr Jan 31 '12 at 03:35
2

Try this

$('.epi-dataTable tr:gt(0) td:first-child:contains("printerName")')
.closest('tr').find('td:eq(4)').text();
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • I tried your script in Chrome's JS console, but the first line seems to return null (I'm substituting prn-hou-mudge-1 for printername, inside the quotes.) – Salem Jan 31 '12 at 05:10
  • That is because you don't have `jQuery` included in your site. Include the jQuery library and try it should work fine. – ShankarSangoli Jan 31 '12 at 15:02
  • Ah, that would do it. Duh. Thanks! – Salem Jan 31 '12 at 19:07
1

If I understood your question correctly, this is how you could do it using jQuery:

$("td").filter(function() { 
    return $(this).text() === yourSeachText;
}).next().next().next().text();

Clarifying: the filter function will select only the column whose text equals your search text, and each call to next will return the column's next sibling - another td. Three calls later, you have the column you want, so you can get its text.

Update: you might also be interested in this question, if the site you're querying is not in the same domain as your script. If it is, you can simply load its contents to a div before querying it:

$("#placeholder").load("http://clusters.andrew.cmu.edu/printerstats/", function() {
    // The contents were loaded to #placeholder, do your query here
});

If it's not, then you'll have to load that html data somehow, and according to an anwser to that question, your best route is really to do it in PHP (or at least use it to echo the other site's contents to your own).

Community
  • 1
  • 1
mgibsonbr
  • 21,755
  • 7
  • 70
  • 112