Results 1 to 2 of 2

Thread: How I get source element's tagName from event?

  1. #1
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation How I get source element's tagName from event?

    Hello all,
    I have a problem that how i get source element from event ‘e’ in following example.

    I given onfocus event on body tag.
    <body onfocus="Event.observe(document, 'keypress', handleKeyboardShortcuts);">

    function for “handleKeyboardShortcuts” is fallows ——-

    function handleKeyboardShortcuts(e)
    {
    var code; if (!e) e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    **
    var sourceElement = e.srcElement.tagName;
    alert(“source element: ”+ sourceElement);
    var character = String.fromCharCode(code);

    //key = 'space bar'
    if(code 32 || character "")
    {
    //I do something here.
    }

    }


    I used e.srcElement.tagName; but i don’t get source element.

    Can any one tell me how i get it?

    Thanks,
    vikas.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Spacebar is reserved in many browsers. It's best not to mess with the keyboard, no matter how tempting. However, this code will get the tag name, if there is one.

    Code:
    function handleKeyboardShortcuts(e){
    e = e || window.event;
    var key, el, character;
    key = e.keyCode ||  e.which; 
    el = e.target || e.srcElement;
    alert('source element: '+ el.tagName); 
    character = String.fromCharCode(key);
    
    //key = 'space bar'
    if(key == 32 || character == " ")
    {
    //I do something here.
    }
    
    };
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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