0

I'm looking for the best way to get a selection expression for an element by passing the element , i.e. : i want the reverse of sizzle/slick ... i want to pass an element i've clicked on and get a unique selection expression for it (e.g. : 'table[0] tr td[5] ') is there a library that does that ? or do i have to build it myself by traverssing the dom ?

Thanks in advance .

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Amnon
  • 1,241
  • 3
  • 10
  • 19
  • 2
    There is an infinite number of "unique" selectors for any given element in the DOM. – BoltClock May 12 '11 at 14:27
  • Plus, there's this cool feature called the ID attribute. – Thomas Shields May 12 '11 at 14:29
  • You're probably looking for *xpath*, include that term in your Googling adventures. – chelmertz May 12 '11 at 14:29
  • Guys your assuming that i have the ability to change the source of the page , i'm well aware of ids etc and i know there could be a large amount of unique selectors , xpath is something id rather avoid i want an engine that generates a css selector from an element – Amnon May 12 '11 at 14:32
  • Well if you can't change the source of the page, that's probably worth mentioning in the question. – Thomas Shields May 12 '11 at 14:32

3 Answers3

0

I think this jQuery plugin will do what you are looking for or at least close to it.

Stuart Burrows
  • 10,824
  • 2
  • 34
  • 33
0

I like chelmertz comment and Thomas Shields.

As a mixture of this, a good approach would be first to check if the element has an ID. If so, use that - as it's always going to be the fastest way to re-access your element.

And if it doesn't return an xpath string.

And alternative, would be to simply assign it an ID.

I believe in prototype.js you can give an element an identity.

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
0

Why are you trying to pass the element? Do you intend on doing something with it? If not, just use jQuery, select all the elements and then choose the one you clicked.

For example, you're talking about table rows or cells above. You could do the following to select a specific table cell:

$('td').click(function(){
    $(this).addClass('selected');
});

or change the td to tr in that code to make it add the class to the row.

tadywankenobi
  • 745
  • 1
  • 10
  • 28