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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Kamermans

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

    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>conten t</tag_2>
    <tag_3>conten t, 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

  • Thomas 'PointedEars' Lahn

    #2
    Re: how do I find out which html tag called a javascript function?

    Mike Kamermans wrote:
    [color=blue]
    > How do I find out from which tag I called a javascript function at
    > runtime?[/color]

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

    You mean nested elements, or elements in an ancestor-descendant
    relationship.
    [color=blue]
    > <tag_1 value="someval" >
    > <tag_2>conten t</tag_2>[/color]
    `--,--'`--,--'`---,--'
    [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
    [color=blue]
    > <tag_3>conten t, javascript link to function modify()</tag_3>[/color]

    <tag_3><... on...="... this ...">...</...></tag_3>
    [color=blue]
    > </tag_1>
    >
    > Someone suggested I use "this" as a function parameter to modify(),[/color]

    Correct.
    [color=blue]
    > 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).[/color]

    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

    Comment

    • Java  script  Dude

      #3
      Re: how do I find out which html tag called a javascript function?

      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.

      Comment

      • Java  script  Dude

        #4
        Re: how do I find out which html tag called a javascript function?

        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

        Comment

        Working...