How to find the ID of the "current" element

P

pamelafluente

Hello. A question.
It is possible to refer to the ID of an element within the element
itself? And, if yes, what is the instruction or function.

Example. Assume I have this:

<div id="Square1" onclick = "Clicked('Square1')"></div>

You see the ID of the element "Square1" also appears later on the
function invocation "Clicked('Square1')". My question is: can I replace
the second occurrence of "Square1" with an instruction indicating the
current ID element? How?


For instance, I assume the instruction existed and were
"CurrentElmID()", I would have:

<div id="Square1" onclick = "Clicked(CurrentElmID())"></div>

Hope my question is clear. Thanks.

-Pam
 
E

Evertjan.

wrote on 23 aug 2006 in comp.lang.javascript:
Hello. A question.
It is possible to refer to the ID of an element within the element
itself? And, if yes, what is the instruction or function.
Example. Assume I have this:

<div id="Square1" onclick = "Clicked('Square1')"></div>

[..]

<div id = 'Square1' onclick = 'alert(this.id)'>
Content
</div>
 
R

Richard Cornford

Evertjan. said:
wrote on 23 aug 2006 in comp.lang.javascript:
Hello. A question.
It is possible to refer to the ID of an element within the element
itself? And, if yes, what is the instruction or function.
Example. Assume I have this:

<div id="Square1" onclick = "Clicked('Square1')"></div>

[..]

<div id = 'Square1' onclick = 'alert(this.id)'>
Content
</div>

Although virtually the only useful thing that can be done with that ID
is to use it to look up a reference to the element in the DOM, and as -
this - is a reference to that element at the point when its - id -
property is read it would be more sensible to pass the reference on the
function called instead of a string ID.

Richard.
 
P

pamelafluente

Thanks Evertjan, very helpful.

Thanks Richard for the comments.
I need it because I prefer to have updatable code in one place only:
more maintenable.

I hope there are no browser compatibility issues with this one :).

Thank you very much.

-Pam

Richard Cornford ha scritto:
Evertjan. said:
wrote on 23 aug 2006 in comp.lang.javascript:
Hello. A question.
It is possible to refer to the ID of an element within the element
itself? And, if yes, what is the instruction or function.
Example. Assume I have this:

<div id="Square1" onclick = "Clicked('Square1')"></div>

[..]

<div id = 'Square1' onclick = 'alert(this.id)'>
Content
</div>

Although virtually the only useful thing that can be done with that ID
is to use it to look up a reference to the element in the DOM, and as -
this - is a reference to that element at the point when its - id -
property is read it would be more sensible to pass the reference on the
function called instead of a string ID.

Richard.
 
E

Evertjan.

Richard Cornford wrote on 23 aug 2006 in comp.lang.javascript:
Evertjan. said:
wrote on 23 aug 2006 in comp.lang.javascript:
Hello. A question.
It is possible to refer to the ID of an element within the element
itself? And, if yes, what is the instruction or function.
Example. Assume I have this:

<div id="Square1" onclick = "Clicked('Square1')"></div>

[..]

<div id = 'Square1' onclick = 'alert(this.id)'>
Content
</div>

Although virtually the only useful thing that can be done with that ID
is to use it to look up a reference to the element in the DOM, and as -
this - is a reference to that element at the point when its - id -
property is read it would be more sensible to pass the reference on the
function called instead of a string ID.

"Going back to square1" should be an option.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,329
Latest member
InezZ76898

Latest Threads

Top