how do I find out which html tag called a javascript function?

M

Mike Kamermans

How do I find out from which tag I called a javascript function at
runtime? I have a stack of nested custom tags, in the following
structure:

<tag_1 value="someval">
<tag_2>content</tag_2>
<tag_3>content, javascript link to function modify()</tag_3>
</tag_1>

Someone suggested I use "this" as a function parameter to modify(), but
that contains the entire window object, rather tha a reference to the
document's html node that called the function (in this case tag_3).

If anyone knows how to include the tag's position in the document so that
I can pull its content and find its parent, etc, I'd be very grateful.

- Mike
www.nihongoresources.com
 
T

Thomas 'PointedEars' Lahn

Mike said:
How do I find out from which tag I called a javascript function at
runtime?

You mean an (HTML?) element, not only a tag. Elements are delimited
by (start and end) tags.
I have a stack of nested custom tags, in the following structure:

You mean nested elements, or elements in an ancestor-descendant
relationship.
<tag_1 value="someval">
<tag_2>content</tag_2>
`--,--'`--,--'`---,--'
[1] [2] [3]
`---------,----------'
[4]

[1] start tag; contains attribute declarations; sometimes optional
[2] element content; may contain text nodes and/or other elements
(see its content model definition)
[3] end tag; sometimes optional
[4] "tag_2" element
<tag_3>content, javascript link to function modify()</tag_3>

</tag_1>

Someone suggested I use "this" as a function parameter to modify(),
Correct.

but that contains the entire window object, rather tha a reference to the
document's html node that called the function (in this case tag_3).

It should not. Without having a look at the *real* markup and at relevant
*snippets* of your code, it is even impossible to guess what went wrong.
And which user agent(s) have you tested with?


PointedEars
 
J

Java script Dude

If you declare the event inline you can pass the object reference to
the handler - in your case modify().

Sample: <tag3 onclick='modify(event,this)'>

With this code, the function modify is called with two arguments: The
event object and a object pointer to the DOM element that triggered
the event (this).

If you referenced this from a method that is globally declared, the
method is by default a method of `window` and as such, `this` will
give you the window object. If you called a method of an object
besides window you will get that object ie
myObj.modify(){alert(this)}. Inline event declarations allow for a
`this` property that points to the source element.

If you instead assign your listeners outside (not inline), there is no
way to guarentee that you can get the object pointer to the
originating object as for some reason, the event object does not have
a source property :[

There is a funky way to get the source a property to work but it's a
little too complex to post here as it requires writing a custom
listener assignment code.
 
J

Java script Dude

Doooh!

I just checked to validate again and looks like I was wrong. The
'this' property of any handler is the originating 'source' element
even if the handler declared is a method of an Object.

Scratch what I said. If you are handling the event, you should be able
to detect the source element with the 'this' property.

JsD
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top