Results 1 to 7 of 7

Thread: Javascript calling object

  1. #1

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464

    Javascript calling object

    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
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  2. #2
    Fanatic Member Psyrus's Avatar
    Join Date
    Jul 2000
    Location
    NJ
    Posts
    602
    Try:

    event.srcElement.style.left=
    Chris

    VB 6.0 Calendar App Video Gamers Group
    Don't forget to rate people if they helped you.

  3. #3

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    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
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  4. #4
    Lively Member benski's Avatar
    Join Date
    Sep 1999
    Location
    UK
    Posts
    126
    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:

    Code:
    function foo(esource) {
    
      alert('Source Element = ' + esource);
    
    }
    If your event is currently called like this:

    Code:
    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:

    Code:
    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:

    Code:
    function foo() {
    
      alert('Source Element ID = ' + event.srcElement.id);
    
    }

    Is this what you wanted?

  5. #5

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    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
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

  6. #6
    Lively Member benski's Avatar
    Join Date
    Sep 1999
    Location
    UK
    Posts
    126
    Surprisingly enough:

    Code:
    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...

    Code:
    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??

  7. #7

    Thread Starter
    Hyperactive Member TiPeRa's Avatar
    Join Date
    Apr 2001
    Location
    In between
    Posts
    464
    Thanks a lot. It works great now.
    -TiPeRa
    W#Ć€V€® W¦|| ߀ W¦|| ߀, ÄÑÐ †#€®€ ¦§ ÑÖ†#¦Ñ6 ¥Öµ ©ÄÑ ÐÖ ÄßÖµ† ¦†, §Ö §¦† ßÄ©K, ®€|ÄX ÄÑÐ |€† ¦† #ÄÞÞ€Ñ.
    (Whatever will be will be, and there is nothing you can do about it, so sit back, relax and let it happen.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width