newbie question: who clicked on me?

O

oddvark

Hi I have a simple question. From a click event, how do I find out
what element triggered it? In delphi there was a property called
"sender" which was the sender.

Heres an example of what I'm doing:

jQuery('#buttonA, #buttonB').bind('click', function()
{
if sender is buttonA
do something
else
do something else.
});


I would like it to be the same function just in one part it has to do
one thing if the sender is something otherwise it does something
else. Thanks!
 
L

Lee

Hi I have a simple question.  From a click event, how do I find out
what element triggered it?  In delphi there was a property called
"sender" which was the sender.

Heres an example of what I'm doing:

    jQuery('#buttonA, #buttonB').bind('click', function()
    {
        if sender is buttonA
            do something
        else
           do something else.
    });

I would like it to be the same function just in one part it has to do
one thing if the sender is something otherwise it does something
else.  Thanks!

I do not know delphi but this sounds similar:

<body>
<input type="button" id="buttona" name="buttona" value="Button A"
onclick="dosomething(this)">
<input type="button" id="buttonb" name="buttonb" value="Button B"
onclick="dosomething(this)">

<script>
function dosomething(o) {
if(o.id == "buttona") {
do something
} else {
do something else
}
}
</script>
</body>
 
R

RobG

Hi I have a simple question. From a click event, how do I find out
what element triggered it? In delphi there was a property called
"sender" which was the sender.

Heres an example of what I'm doing:

jQuery('#buttonA, #buttonB').bind('click', function()

If you want information specifically about jQuery, ask in a jQuery
forum. If you want to know how to do it generically using javascript,
then this is the place.

{
if sender is buttonA
do something
else
do something else.
});

I would like it to be the same function just in one part it has to do
one thing if the sender is something otherwise it does something
else.

The event object related to the click event has properties of either
target (W3C event model) or srcElement (IE event model).

There is a good introduction to events at the link below that will
give you enough to get started:

<URL: http://www.quirksmode.org/js/introevents.html >
 
O

oddvark

Unfortunately I cant pass in the object as a parameter to the call.
The click function takes no parameters.
 
O

oddvark

If you want information specifically about jQuery, ask in a jQuery
forum. If you want to know how to do it generically using javascript,
then this is the place.



The event object related to the click event has properties of either
target (W3C event model) or srcElement (IE event model).

There is a good introduction to events at the link below that will
give you enough to get started:

<URL:http://www.quirksmode.org/js/introevents.html>

Thanks Rob. Let me try the srcElement property.
 
L

Lee

Thanks Rob.  Let me try the srcElement property.- Hide quoted text -

- Show quoted text -

oh, ok try:

<body>
<input type="button" id="buttona" name="buttona" value="Button A"
onclick="dosomething()">
<input type="button" id="buttonb" name="buttonb" value="Button B"
onclick="dosomething()">

<script>
function dosomething() {
if(event.srcElement.id == "buttona") {
do something
} else {
do something else
}
}
</script>
 
S

Stanimir Stamenkov

Tue, 3 Jun 2008 15:44:50 -0700 (PDT), /oddvark/:
Hi I have a simple question. From a click event, how do I find out
what element triggered it? In delphi there was a property called
"sender" which was the sender.

Heres an example of what I'm doing:

jQuery('#buttonA, #buttonB').bind('click', function()
{
if sender is buttonA
do something
else
do something else.
});

As others have noted the W3C standard Event [1] interface has a
'target' property. IE's event [2] however doesn't support it but
has a 'srcElement' serving the same purpose. As you seem to use
jQuery you may take a look at its documentation [3]. There you'll
find it normalizes this difference allowing you to use it uniformly.

[1] <http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event>
[2] <http://msdn.microsoft.com/en-us/library/ms535863(VS.85).aspx>
[3] <http://docs.jquery.com/Types#Event>,
<http://docs.jquery.com/Types/Event>
 
T

Thomas 'PointedEars' Lahn

Lee said:
oh, ok try:

I don't have to in order to know that it will break.
<body>
<input type="button" id="buttona" name="buttona" value="Button A"
onclick="dosomething()">
<input type="button" id="buttonb" name="buttonb" value="Button B"
onclick="dosomething()">

<script>

Not Valid --> http://validator.w3.org/
function dosomething() {
if(event.srcElement.id == "buttona") {

This will break with a TypeError in !(MSHTML || Opera) because `event'
cannot be resolved. Even if it could because someone would have passed
the local event object reference as an argument called so, it would break
with a TypeError because it would not have the MSHTML-proprietary
`srcElement' property and so undefined.id could not be resolved.

Please trim your quotes to the minimum required to retain context.


PointedEars
 
S

Stanimir Stamenkov

Tue, 3 Jun 2008 17:43:39 -0700 (PDT), /oddvark/:
Unfortunately I cant pass in the object as a parameter to the call.
The click function takes no parameters.

You're wrong. Check with your documentation
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Lee said:
function dosomething() {
if(event.srcElement.id == "buttona") {

This will break with a TypeError in !(MSHTML || Opera) because `event'
cannot be resolved. [...]

Correction: It will be a ReferenceError.


PointedEars
 
Á

Álvaro G. Vicario

oddvark escribió:
Hi I have a simple question. From a click event, how do I find out
what element triggered it? In delphi there was a property called
"sender" which was the sender.

Heres an example of what I'm doing:

jQuery('#buttonA, #buttonB').bind('click', function()
{
if sender is buttonA
do something
else
do something else.
});

In jQuery I believe it is:

$(this)

I can't find the exact documentation page but I've seen it in some of
the examples like:

http://docs.jquery.com/Events/bind#typedatafn
 

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

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,365
Latest member
BurtonMeec

Latest Threads

Top