/    Sign up×
Community /Pin to ProfileBookmark

unique identifier for HTMLElement that works across browsers?

Any help much appreciated.

Thank you
Misha

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 07.2010 — Too vague. What kind of element? Tag? Text? Comment? Attribute?

Tag elements can be identified by:

id - [B]getElementById('theid')[/B]

name - [B]getElementsByName('thename')[[I]position in DOM collection[/I]][/B] - [I]only for certain elements[/I]

tag name - [B]getElementsByTagName('tagname')[[I]position in DOM collection[/I]][/B]

tag name with wild card - [B]getElementsByTagName('*')[[I]position in DOM collection[/I]][/B]

position as child node - [B]childNodes[[I]position in DOM collection[/I]][/B]

if first child node - [B]firstChild[/B]

if last child node - [B]lastChild[/B]

sibling position- [B]nextSibling, previousSibling[/B]

Can you detail and elaborate a little bit more your question?
Copy linkTweet thisAlerts:
@misha680authorMay 07.2010 — Sorry, I'll be more precise.

Once I find an element (or any node), I want to be able to access a property access such that, if:
<i>
</i>var a=x.property;

or
<i>
</i>var a=x.method();

then I can uniquely find this element by doing something like
<i>
</i>var element=x.findByUnique(a);


Thank you!

Misha
Copy linkTweet thisAlerts:
@KorMay 07.2010 — You can't. At least not in this way.

It looks like your logic is contradictorily. Once you found an element and gave it a property, that means you referred it somehow, thus why to find it again using that property as an identifier? [I]You already have the reference of that element somewhere[/I], otherwise you could not have given it that property, could you?

Do I miss something? Can you exemplify your aim with a concrete HTML element?
Copy linkTweet thisAlerts:
@misha680authorMay 07.2010 — Your logic would make perfect sense except the framework I am using to execute Javascript is the Java SWT (IBM) Browser object, which does not remember objects between execute commands.

Hence, every time, I must re-find the object.

Somehow, references are not kept ?

Thank you

Misha
Copy linkTweet thisAlerts:
@mrhooMay 07.2010 — If an object doesn't have an id, you can assign one the first time you look at it.
[CODE]
document.uids=1;
document.hoozit= function hoozit(obj){
if(obj.nodeType!=1) return '';
if(!obj.id) obj.id= 'uid_'+document.uids++;
return obj.id;
}[/CODE]
Copy linkTweet thisAlerts:
@KorMay 10.2010 —  which does not remember objects between execute commands.[/QUOTE]
Not very clear to me, but if those commands change the session, you should probably use a cookie. Or a server-side session variable. JavaScript is a client-side language - it does not keep nor track the changes between sessions.
Copy linkTweet thisAlerts:
@misha680authorMay 10.2010 — Thank you. My apologies if unclear my program is on the _browser side_ so now way (?) to set cookies on the server.

In other words I _execute Javascript_ from the browser side to traverse/manipulate the DOM model.

I will have to double check if assigning IDs to elements will work when finding the object... in other words, I understand the ID will be assigned, but not sure it will be retained by my next call to the Browser's "execute Javascript" command.

This is the browser object in SWT btw if you are interested.

http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/browser/Browser.html

www.mkosh.com has the framework I am developing (Open Source) as well as a (rudimentary) Javascript Tester.

Thank you

Misha
×

Success!

Help @misha680 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...