Setting onmousedown as a property value

W

walkerfx

Hi All,

Is there a way to access the value assigned to onmousedown for an
object? I've been searching online but can only seem to find how it is
assigned via html. What I need to do is access the value (the script
string) in javascript.

For example, I'd like to try to do something like this:

function virtualClick(objectName) {
eval(document.getElementById(objectName).onmousedown);
}

I am of course not explaining the full problem in detail, but would
appreciate any input on how to access the onmousedown value (or to know
if it even exists as a property).

Thank you,

Walker
 
M

Martin Honnen

walkerfx wrote:

function virtualClick(objectName) {
eval(document.getElementById(objectName).onmousedown);

If you want to call the event handler then do that e.g.
var element = document.getElementById(objectName);
if (element != null && element.onmousedown != null) {
element.onmousedown();
}
The event handler (if it is set) is a function property and you simply
call that as shown and not with using eval.
 
A

ASM

walkerfx a écrit :
Is there a way to access the value assigned to onmousedown for an
object? I've been searching online but can only seem to find how it is
assigned via html. What I need to do is access the value (the script
string) in javascript.

For example, I'd like to try to do something like this:

function virtualClick(objectName) {
eval(document.getElementById(objectName).onmousedown);
}

function virtualClick(objectId) { // and NOT object_NAME

alert(document.getElementById(objectId).getAttribute('onmousedown'));
}
I am of course not explaining the full problem in detail, but would
appreciate any input on how to access the onmousedown value (or to know
if it even exists as a property).

<html>
<head>
<script type="text/javascript">
function tellMe(e) {
e = e.target? e.target : e.srcElement;
if(e.getAttribute('onmouseover'))
alert(e.getAttribute('onmouseover'));
else alert('no');
}
</script>
</head>
<body onmousedown="tellMe(event)">
<p id=ici
onmouseover="x = this.innerHTML; this.innerHTML='What's matter ?';"
onmouseout="this.innerHTML=x;">
hello
</p>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top