0

I am trying to write HTML annotation service. So I want to attach an annotation to HTML elements where a user clicked. So far this post helped me find HTML element. However I want to locate the element back in DOM tree.

So if I traverse DOM tree down until I met the element, will be my traverse path uniquely identify the element, so I can restore annotation each time, or browsers can build DOM tree differently and there is a risk of changing browser can bring me in trouble?

Another option can be look for position of found element innerHTML in BODY.innerHTML. However if I have repeatable elements I can't provide exact mapping. Any other suggestions? Please do not advise to add some unique id for every HTML element and then use it. It will bloat HTML and I want to avoid it.

Community
  • 1
  • 1
Singagirl
  • 465
  • 1
  • 3
  • 11

1 Answers1

0

Suggest you use jquery.

Can then cycle through each matching element and use this to address the one you want when you find it.

Example:

$('.class-name-of-element').each(function() {
    $(this).css('border','1px solid red'); // or whatever you want to do with it
})
Peter Oram
  • 6,213
  • 2
  • 27
  • 40