PDA

View Full Version : How I get source element's tagName from event?



vikasg
03-09-2009, 10:44 AM
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.

jscheuer1
03-09-2009, 04:17 PM
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.


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.
}

};