PDA

Click to See Complete Forum and Search --> : Javascript calling object


TiPeRa
Jul 31st, 2001, 11:55 AM
How can I get the name of the object calling a procedure through a DHTML event. So
that I can do the following:

CallingObject.style.left=XXX

At the moment I have a seperate procedure for each object.
TIA
-TiPaRa
:D ;) :p

Psyrus
Jul 31st, 2001, 08:10 PM
Try:

event.srcElement.style.left=

TiPeRa
Aug 1st, 2001, 06:44 AM
Hmmm, It sort of works. How can I get the type of object it returns. I tried event.srcElement.type but it just returns [object].
I need it to return the name of a table when the top cell is clicked.
TIA again
-TiPeRa
:D ;) :p

benski
Aug 1st, 2001, 09:14 AM
You could do this (I'll show a possibility later), but I recommend you just alter the function definition to include an argument for the source of the event:


function foo(esource) {

alert('Source Element = ' + esource);

}


If your event is currently called like this:


Example 1:

<table>
<tr>
<td id='elt1' onclick="foo()">Something</td>
</tr>
<tr>
<td id='elt2' onclick="foo()">Something</td>
</tr>
</table>


Just alter the function calls to be:


Example 2:

<table>
<tr>
<td id='elt1' onclick="foo('elt1')">Something</td>
</tr>
<tr>
<td id='elt2' onclick="foo('elt2')">Something</td>
</tr>
</table>


The code above should work cross browser/platform

Or... (possibly IE version specific)

Assuming you have the code as in Example 1, alter your function to be:


function foo() {

alert('Source Element ID = ' + event.srcElement.id);

}



Is this what you wanted?

TiPeRa
Aug 1st, 2001, 09:46 AM
Sorted, (maybe) just one more thing. How do I get the parent of the object:

<table id="tbltest">
<tr>
<td id='tdtest' onclick="foo()">Something</td>
</tr>
</table>

In this situation I need to get the tbltest object when tdtest is clicked. I could probably find it somewhere on MSDN but you're a lot more help ful.
-TiPeRa
:D ;) :p

benski
Aug 1st, 2001, 10:07 AM
Surprisingly enough:


function foo() {

alert('Source Element ID = ' + event.srcElement.parentElement.id);

}


This will give the id of the TR element which contains the TD. Here is the really surprising bit...


function foo() {

alert('Source Element ID = ' + event.srcElement.parentElement.parentElement.parentElement.id);

}


This code gives the parent of the parent of the parent, which is the TABLE containing the TBODY containing the TR containing the TD

Cool?? :D

TiPeRa
Aug 1st, 2001, 02:11 PM
Thanks a lot. It works great now.
-TiPeRa
:D ;) :p