newbie question: who clicked on me?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • oddvark

    newbie question: who clicked on me?

    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('#button A, #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!
  • Lee

    #2
    Re: newbie question: who clicked on me?

    On Jun 3, 4:47 pm, oddvark <dgrd...@yahoo. comwrote:
    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('#button A, #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="dosome thing(this)">
    <input type="button" id="buttonb" name="buttonb" value="Button B"
    onclick="dosome thing(this)">

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

    Comment

    • RobG

      #3
      Re: newbie question: who clicked on me?

      On Jun 4, 7:47 am, oddvark <dgrd...@yahoo. comwrote:
      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('#button A, #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 >


      --
      Rob

      Comment

      • oddvark

        #4
        Re: newbie question: who clicked on me?

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

        Comment

        • oddvark

          #5
          Re: newbie question: who clicked on me?

          On Jun 3, 3:44 pm, RobG <rg...@iinet.ne t.auwrote:
          On Jun 4, 7:47 am, oddvark <dgrd...@yahoo. comwrote:
          >
          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('#button A, #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.htm l>
          >
          --
          Rob
          Thanks Rob. Let me try the srcElement property.

          Comment

          • Lee

            #6
            Re: newbie question: who clicked on me?

            On Jun 3, 7:46 pm, oddvark <dgrd...@yahoo. comwrote:
            On Jun 3, 3:44 pm, RobG <rg...@iinet.ne t.auwrote:
            >
            >
            >
            >
            >
            On Jun 4, 7:47 am, oddvark <dgrd...@yahoo. comwrote:
            >
            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('#button A, #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.htm l>
            >
            --
            Rob
            >
            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="dosome thing()">
            <input type="button" id="buttonb" name="buttonb" value="Button B"
            onclick="dosome thing()">

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

            Comment

            • Stanimir Stamenkov

              #7
              Re: newbie question: who clicked on me?

              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('#button A, #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#Eve nts-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>

              --
              Stanimir

              Comment

              • Stanimir Stamenkov

                #8
                Re: newbie question: who clicked on me?

                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
                <http://docs.jquery.com/Events/click#fn>:
                Arguments:
                fn Function
                A function to bind to the click event on each of the matched
                elements.
                >
                function callback(eventO bject) {
                this; // dom element
                }
                --
                Stanimir

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: newbie question: who clicked on me?

                  Thomas 'PointedEars' Lahn wrote:
                  Lee wrote:
                  >function dosomething() {
                  > if(event.srcEle ment.id == "buttona") {
                  >
                  This will break with a TypeError in !(MSHTML || Opera) because `event'
                  cannot be resolved. [...]
                  Correction: It will be a ReferenceError.


                  PointedEars
                  --
                  var bugRiddenCrashP ronePieceOfJunk = (
                  navigator.userA gent.indexOf('M SIE 5') != -1
                  && navigator.userA gent.indexOf('M ac') != -1
                  ) // Plone, register_functi on.js:16

                  Comment

                  • =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=

                    #10
                    Re: newbie question: who clicked on me?

                    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('#button A, #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://alvaro.es - Álvaro G. Vicario - Burgos, Spain
                    -- Mi sitio sobre programación web: http://bits.demogracia.com
                    -- Mi web de humor al baño María: http://www.demogracia.com
                    --

                    Comment

                    Working...