Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how do I find out which html tag called a javascript function?

Reply
Thread Tools

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

 
 
Mike Kamermans
Guest
Posts: n/a
 
      08-10-2004
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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-11-2004
Mike Kamermans wrote:

> 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_3><... on...="... this ...">...</...></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
 
Reply With Quote
 
 
 
 
Java script Dude
Guest
Posts: n/a
 
      08-11-2004
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.
 
Reply With Quote
 
Java script Dude
Guest
Posts: n/a
 
      08-12-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to find <tag> to </tag> HTML strings and 'save' them? mark@agtechnical.co.uk Python 7 03-26-2007 04:11 PM
HTML.Tag.BASE same as HTML.Tag.A carlbernardi@gmail.com Java 1 11-29-2006 03:41 AM
WebService called by automation dll times out when being called from Navision Felix ASP .Net Web Services 1 09-29-2006 01:43 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM



Advertisments