By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,375 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

know if is a click or onchage the action on a select

P: n/a
In a select , if I want use the same function on the click and onchange
events; how I can write a code inner the function that can know wich events
have activate itself?

Jun 17 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Jun 17, 2:22 pm, artev <mailnotspa...@notspamm.nnwrote:
In a select , if I want use the same function on the click and onchange
events; how I can write a code inner the function that can know wich events
have activate itself?
In a way it sounds like you don't want to run the same function. If
you did then you wouldn't need to know the even type. It might be
better to have two handlers that call a third common function.

However, to answer your question...

You can pass a string to the handler function

onclick="foo('click');" onchange="foo('change');"

function foo(eventType) {
alert(eventType);
}
Or probably better you can pass the event to the handler function and
look at the type of event

onclick="foo(event);" onchange="foo(event);"

function foo(event) {
alert(event.type); // will be "click" or "change"
}
Peter

Jun 17 '07 #2

P: n/a
Or probably better you can pass the event to the handler function and
look at the type of event


onclick="foo(event);" onchange="foo(event);"
function foo(event) {
alert(event.type); // will be "click" or "change"
}
I have this:
... onclick="foo(this);" onchange="foo(this);

function foo(parameter) {
alert(event.type); // will be "click" or "change"

paramaeter use here ....

}
work only on IE; on firefox not work
Jun 18 '07 #3

P: n/a
On Jun 18, 12:24 am, artev <mailnotspa...@notspamm.nnwrote:
I have this:
.. onclick="foo(this);" onchange="foo(this);

function foo(parameter) {
alert(event.type); // will be "click" or "change"

paramaeter use here ....

}
Use:
... onclick="foo(event,this);" onchange="foo(event,this);
function foo(ev,parameter) {
ev=ev||event;
alert(ev.type); // will be "click" or "change"

paramaeter use here ....

}

Jun 18 '07 #4

P: n/a
On Jun 18, 9:28 am, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On Jun 18, 12:24 am, artev <mailnotspa...@notspamm.nnwrote:
I have this:
.. onclick="foo(this);" onchange="foo(this);
function foo(parameter) {
alert(event.type); // will be "click" or "change"
paramaeter use here ....
}

Use:
.. onclick="foo(event,this);" onchange="foo(event,this);
function foo(ev,parameter) {
ev=ev||event;
This all looks good but I think the above line is not necessary.
"event" is available to handler attributes in both IE and others.
alert(ev.type); // will be "click" or "change"

paramaeter use here ....

}
Peter

Jun 18 '07 #5

P: n/a
Peter Michaux wrote:
On Jun 18, 9:28 am, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
>On Jun 18, 12:24 am, artev <mailnotspa...@notspamm.nnwrote:
>>I have this:
.. onclick="foo(this);" onchange="foo(this);
function foo(parameter) {
alert(event.type); // will be "click" or "change"
paramaeter use here ....
}
Use:
.. onclick="foo(event,this);" onchange="foo(event,this);
function foo(ev,parameter) {
ev=ev||event;

This all looks good but I think the above line is not necessary.
"event" is available to handler attributes in both IE and others.
Not only that, but "event" will be undefined within the scope of a
function. If I remember correctly it only gets assigned via an event
handler.

Thereby, if foo(null, this); is used, "event" cannot be assigned to
"ev," again, because it is undefined within the function.
> alert(ev.type); // will be "click" or "change"

paramaeter use here ....

}
--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 18 '07 #6

P: n/a
On Jun 18, 1:07 pm, -Lost <maventheextrawo...@techie.comwrote:
Peter Michaux wrote:
On Jun 18, 9:28 am, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On Jun 18, 12:24 am, artev <mailnotspa...@notspamm.nnwrote:
>I have this:
.. onclick="foo(this);" onchange="foo(this);
function foo(parameter) {
alert(event.type); // will be "click" or "change"
paramaeter use here ....
}
Use:
.. onclick="foo(event,this);" onchange="foo(event,this);
function foo(ev,parameter) {
ev=ev||event;
This all looks good but I think the above line is not necessary.
"event" is available to handler attributes in both IE and others.

Not only that, but "event" will be undefined within the scope of a
function. If I remember correctly it only gets assigned via an event
handler.

Thereby, if foo(null, this); is used, "event" cannot be assigned to
"ev," again, because it is undefined within the function.
Lost*,

In Internet Explorer "event" is a global variable (or window object
property, depending on how you look at it) for the event object
currently being handled. So there is something to what
scripts.contact* wrote but it isn't necessary in this case because
"event" is available in the handler attribute (but for different
reasons in IE vs others.)

* You guys do have real names assigned by your parents, right?

Peter

Jun 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.