Jump to content

Fundamental Object IDs in Javascript


kael.shipman

Recommended Posts

Hi everyone,

 

Does anyone know if there's an low-level, readonly object ID that can be accessed in javascript for any and all objects across the board? It seems to me that all objects - be it an Array, Object, Image, or from the entire arsenal of HTML elements - should already have an ID that javascript uses to organize them internally. My problem is that I want to uniquely identify HTML objects without having to do a check/write-if-not-exists sequence on the ID attribute of the element. This doesn't apply to any code specifically, but here's an idea:

 

Here, we're putting all of the original text of all the divs on the page - some of which may have html ids but most of which won't - into a hash with the div object's fundamental object id as the key and the text as the value. Then, even if the html id attribute is changed for some reason, that div will always be associated with the text that was cataloged on the pageload. Remember, this doesn't have any practical application, but is a clear example of what I want.

 

window.onload = function() {
     window.allDivs = {}
     var docDivs = document.getElementsByTagName('div');
     var uniqueId;
     for(var i = 0; i < docDivs.length; i++) {
          uniqueID = docDivs[i].lowLevelObjectId;
          //Note that this is not the simple html ID attribute, since most divs won't have that attribute set

          window.allDivs[uniqueID] = docDivs[i].innerHTML;
     }
}

//Now, assume that people can change the content of whatever div,
//so maybe they've changed the content and want to see what was in it originally.
//Maybe double-clicking the div calls viewHistory, sending the HTMLDivObject itself as the argument

function viewHistory(thisDiv) {
     history = window.allDivs[thisDiv.lowLevelObjectId];
     alert(history);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.