views:

223

answers:

2

I currently have a website source code (no control over the source) which contains certain content that needs to be manipulated. This would be simple on the surface, however there is no unique ID attribute on the tag in question that can uniquely identify it, and therefore allow for further traversal.

Here is a snippet of the source code, surrounding the tag in question.

   ...
    <td width="100%"> 

     <!--This table snaps the content columns(one or two)--> 

     <table border="0" width="100%"> 
   ...

Essentially, the HTML comment stuck out as an easy way to gain access to that element. Using the JQuery comment add-on from this question, and some help from snowlord comment below, I have been able to identify the comment and retrieve the following output using the 'dump' extension.

$('td').comments().filter(":contains('This table snaps the content columns(one or two)')").dump();

returns;

jQuery Object {   
    0 = DOMElement [ 
        nodeName: DIV
        nodeValue: null
        innerHTML: [ 
            0 = String: This table snaps the content columns(one or two)
        ]
    ] 
}

However I am not sure how to traverse to the sibling element in the DOM.

This should be simple, but I haven't had much selector experience with JQuery. Any suggestions are appreciated.

A: 

If you have any control over this code at all, put an id on the table.

If you have to do something nasty, perhaps you could use

var tables = $("table");

To get all tables and then ascertain the table in question by index...

$(tables[5]).css("color", "aqua");

This breaks as soon as a table is added or removed, but if you cannot add an id to the table it seems like a potential alternative.

Sohnee
I would _love_ to put an ID on the table :)Unfortunately I don't have any control over the code :(I wanted to avoid hard-coding as much as possible (as you said, it will break for future table changes). Thanks for taking the time to comment though!
Shadow
A: 

I am thinking you could use the siblings method:

$('td').comments().siblings('table').yourcode...

From the docs:

Given a jQuery object that represents a set of DOM elements, the .siblings() method allows us to search through the siblings of these elements in the DOM tree and construct a new jQuery object from the matching elements.

The method optionally accepts a selector expression of the same type that we can pass to the $() function. If the selector is supplied, the elements will be filtered by testing whether they match it.

Peter Jaric
Maybe not exactly the code above, since that would get all tables that are siblings to any comments.
Peter Jaric
Hmm, unfortunately there are multiple instances of a comment with a sibling as a table. Is there any way I can filter the comments() by the text they contain? e.g. when I dump the comments() array, there are several attributes with .innerHTML -- which contain the text I could filter by. Thoughts?
Shadow
Yes, try the `:contains` selector: http://api.jquery.com/contains-selector/ which you would use like this: `$("td").comments().filter(":contains('your text')").siblings('table').yourcode...`
Peter Jaric
Or something like that, I didn't test it.
Peter Jaric
1 Step closer, the only problem now is accessing the sibling. JQuery doesn't seem to know the <table> is a sibling of the comment...
Shadow
Yes, see my comment to your question. I think that the comments method returns newly created divs that are not placed at the correct place in the DOM. This is just a theory, I haven't thoroughly looked through the source code.
Peter Jaric
But the comments() docs say something about it setting the rel attribute to it parent's ID. So you could probably get the comment (with the filter code) and then find out its parent ID, then use it to get the parent, call children('table') on that, and then go on with your code. TO do all that, I think you need to read som jQuery docs :)
Peter Jaric