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

Discussion in 'Javascript' started by Mike Kamermans, Aug 10, 2004.

  1. 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
     
    Mike Kamermans, Aug 10, 2004
    #1
    1. Advertising

  2. 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
     
    Thomas 'PointedEars' Lahn, Aug 11, 2004
    #2
    1. Advertising

  3. 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.
     
    Java script Dude, Aug 11, 2004
    #3
  4. 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
     
    Java script Dude, Aug 12, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    931
    John C. Bollinger
    Jan 27, 2006
  2. Replies:
    1
    Views:
    421
    Andrew Thompson
    Nov 29, 2006
  3. Replies:
    7
    Views:
    510
    Max Erickson
    Mar 26, 2007
  4. Mark Sullivan
    Replies:
    0
    Views:
    282
    Mark Sullivan
    Dec 21, 2007
  5. Felix
    Replies:
    1
    Views:
    361
    Felix
    Sep 29, 2006
Loading...

Share This Page