Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Dec 2004
    Location
    Essex, UK
    Posts
    2,636
    Thanks
    0
    Thanked 0 Times in 0 Posts

    event.srcElement

    Can anyone explain why this just alerts [object]. I'm trying to change the background of a tr when it's clicked on, I only added in the alerts to see why it wasn't working.

    <tr class="a" onclick="sel()">

    function sel() {
    var x = event.srcElement;
    alert(x);
    }


    Is there a better method?

  • #2
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    I usually just pass the object with the call of the function:

    Code:
      function sel(obj)
      {
    	alert(obj);
      }
      ____________
    
    <input type="button" onclick="sel(this);" />
    Whether or not that's better, I couldn't say.

    As far as why it's alerting an object, is simple. The element "tr" is an object, it being the source element of the event. If you wanted to know the type of object or the object tag name, you could do something like this:

    Code:
      function sel(obj)
      {
    	alert(obj.tagName);
        alert(obj.getAttribute('type'));
      }
    The object reference you are left with is the same as saying:

    Code:
    function go()
    {
      var obj=document.getElementById('objID');
      alert(obj);
    }
    Edit:
    Oh, and to change the background-color:
    Code:
    function go(obj)
    {
      obj.style.backgroundColor='#336699';
    }
    Also note, if you are changing the background color of a tr element, and the td element has a background color too, the td element's color will show up over the tr elements color.

    Basscyst
    Last edited by Basscyst; 01-27-2006 at 09:31 PM.
    Helping to build a bigger box. - Adam Matthews


  •  

    Posting Permissions

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