Take the 2-minute tour ×

I am getting element with event.target or event.srcElement.

What information of event can I save to know exactly the previously element selected, without set a id?

If I close the page, how to get the same element?

Save all parentNode seems to be a very slow way to get the element again.

Is there a more efficient way?

The page will contain a fixed number of elements. But this page can not be changed, I could not include the id attribute. However, I would mark the last tag that was clicked when the user open the page again.

share|improve this question
Just to be clear, this doesn't point to the element you need (in the scope of your event handler)? –  cheeken Jan 25 '12 at 1:18
When you close the page, and come back to it, obviously all of the elements will have been recreated and therefore the only way to accomplish this is some sort of unique id. What exactly are you trying to do? –  SoWeLie Jan 25 '12 at 1:20
The most efficient way is almost certainly setting and id. Why the prohibition against the simplest solution? –  Jordan Jan 25 '12 at 1:23
@cheeken I dont understand. –  Cobaia Jan 25 '12 at 1:33
@SoWeLie I added more information to the question –  Cobaia Jan 25 '12 at 1:34
show 6 more comments

3 Answers

The best way, based on your requirements, is to generate some sort of xpath based on class names and if they don't exist, element names. If you don't have an id that is the only way to get some sort of unique identifier for the element. See my jsfiddle for an example.

var getNodeIdentifier = function(element) {
    return element.nodeName.toLowerCase() + "." + $(element).index();

var current = element;
var xpath = getNodeIdentifier(this);

while (current.parentNode != null) {
    xpath = getNodeIdentifier(current.parentNode) + "/" + xpath;
    current = current.parentNode;



Please note, the only problem is if none of your elements have any classes, this will probably not work because if you have multiple elements at the same level with the same node type, there is no way to uniquely identify them.

share|improve this answer
The page will have multiple elements with the same nodes at the same level. :( How about offset position, the page will be used in only one browser. I liked jsfiddle page +1 –  Cobaia Jan 25 '12 at 11:02
Hmm...yes you may be able to use the index within the parents. Do you have the ability to include jquery in this page? –  SoWeLie Jan 25 '12 at 11:55
I will use the chrome. Forgot to mention that I do not know much about javascript, and do not know what is jquery, I'll read about it. –  Cobaia Jan 25 '12 at 11:59
Yes, I can use jquery. –  Cobaia Jan 25 '12 at 12:14
JQeury makes it easy to get at the ordinal of the node, I will update the jsfiddle. –  SoWeLie Jan 25 '12 at 13:23
show 3 more comments

I need something similar and I am wondering what could go wrong with using css selectors (apart from older browser compatibility).

Something like:



One could easily build such a selector following the path up to the body.

function structureSelector(element) {
  var tagName = element.tagName.toLowerCase();
  if (tagName == 'body') {
    return tagName;
  var indexInParent = Array.prototype.indexOf.call(element.parentNode.childNodes, element);
  return structureSelector(element.parentNode)+'>'+tagName+':nth-child('+(indexInParent+1)+')';


share|improve this answer
add comment
up vote 0 down vote accepted

I found an interesting way.

To save element:

// Save node name and position with this
// el = elemento
var p = document.getElementsByTagName(nodeName);
    if( el == p[i])
        return i;

To get:

// With nodeName saved and "position" i
var elem = document.getElementsByTagName(nodeName)[i];
share|improve this answer
add comment

Your Answer


