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.

3
  • 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. Commented Jan 31, 2012 at 3:19
  • All of the functions here seem to return null after attempting to filter the element out. Commented Jan 31, 2012 at 5:11
  • (Oh, and I'm running them on the actual page, not trying to load them yet). Commented Jan 31, 2012 at 5:20

3 Answers 3

2

Try this

$('.epi-dataTable tr:gt(0) td:first-child:contains("printerName")')
.closest('tr').find('td:eq(4)').text();
Sign up to request clarification or add additional context in comments.

3 Comments

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.)
That is because you don't have jQuery included in your site. Include the jQuery library and try it should work fine.
Ah, that would do it. Duh. Thanks!
2
var printerName = 'yourname',
    status = $(':contains(' + printerName + ')').siblings().eq(2).text();

1 Comment

I think the :contains 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...)
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).

1 Comment

@ShankarSangoli updated the answer with links to the relevant documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.