Web Analytics Made Easy -
StatCounter add a function to an event - CodingForum

Announcement

Collapse
No announcement yet.

add a function to an event

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • add a function to an event

    I use this code to add a function for the onchange event:

    obj.onchange = function () { calculateExpr(expression,fieldType) }

    but how can I add this function without deleting the preceding functions defined in this event?
    I tried with += but it doesn't work...

    Thanks

  • #2
    if your funtions dont return anything then oyu could try this

    function changeevent()
    {
    //first function you want to put in
    [etc...]

    //second function you wanted to put in
    [etc...]

    //third function you wanted to put in
    [etc...]
    }

    obj.onchange = changeevent()
    photoshop too expensive? use the GIMP! www.gimp.org

    Comment


    • #3
      Well....

      It should be something like this:


      obj.onchange = function () { obj.onchange ;calculateExpr(expression,fieldType) }


      but how can obj.onchange be interpreted as the content of hum ..."obj.onchange"? Is it clear?

      Comment


      • #4
        sorry i dont follow what you mean

        but how can obj.onchange be interpreted as the content of hum ..."obj.onchange"? Is it clear?
        photoshop too expensive? use the GIMP! www.gimp.org

        Comment


        • #5
          Ok...

          I'll try to be clearer:

          I have an object which is composed of an onchange event.
          This event is composed of function A.

          Now I need to add dynamically a second function to the object's onchange event: function B.

          So I do this:

          obj.onchange = function() {functionB;};

          But in this case function A is logically deleted, so my question is how can I add function B and keep function A in the event?

          Comment


          • #6
            obj.addEventListener('change', myListener, false);
            // for Gecko (NS6, NS7, Mozilla, Beonex, K-meleon, and Galeon), and other DOM2 browsers

            obj.attachEvent('onchange', myListener);
            // for IE

            There is one way, or you could try:

            obj.oldOnchange = obj.onchange;
            obj.onchange = function() {
            //my normal code
            obj.oldOnchange();
            }
            jasonkarldavis.com

            Comment


            • #7
              Ok thanks

              I have a last question about the 2 methods: addEventListener and attachEvent, which one corresponds to a JavaScript standard? Is it IE's method or Gecko's method?

              Comment


              • #8
                I found the answer (IE proprietary)

                Comment


                • #9
                  Gecko's method of course - addEventListener is defined in the DOM2 Events specs.

                  document.implementation.hasFeature('Events','2.0')

                  is true in Gecko, false in IE.
                  jasonkarldavis.com

                  Comment

                  Working...
                  X