473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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>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
Jul 23 '05 #1
3 2043
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
Jul 23 '05 #2
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.
Jul 23 '05 #3
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
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Knut | last post by:
Hello everybody. This is my first attempt at Javascript, something that is long overdue. But please bear that in mind, and have mercy (= My question is simple: I'm doing a dropdown menu...
1
by: Anna K. | last post by:
Hi Experts, I'm new to JavaScript and web-based apps development, so I'll tell you right off that I don't really know my way around it as of yet. I'm trying to create a code library set with...
1
by: Shawn McNiven | last post by:
Hello, I have a problem with an application i'm working on (Asp.net using vb.net for developing). I'm normally used to doing middle tier development, so my javascript skills are not up to par. ...
16
by: Java script Dude | last post by:
Creating a method of object (Object.prototype.classOf ...) is not the correct way because of a (as expected) flaw in IE where DOM Elements does not inherit from Object. As a result it is best to...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.