Javascript Object Attach event not working

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bhatiaajay@gmail.com

    Javascript Object Attach event not working

    I am new to JavaScript objects. The object creates a button with no
    events attached. Later I want to attach an event to the button created
    by this object. The event attaches a function in the object.

    The code is listed below but the event is never attached.

    The 'this' reference for the button seem to be correct and the
    'self'
    reference to the control(object) also seems to be correct.

    What am I doing wrong?


    <html>
    <script>

    // control
    function oeventControl() {}

    oeventControl.p rototype.paint = function(canvas ){
    this.mainBody = document.create Element("div");

    this.mainBody.a ppendChild(docu ment.createElem ent("input"));
    this.button = this.mainBody.g etElementsByTag Name("input")[0];
    this.button.set Attribute("TYPE ","BUTTON") ;
    this.button.set Attribute("VALU E","click me after attaching
    event");
    this.button.sty le.color="red";

    canvas.innerHTM L= this.mainBody.o uterHTML;

    }

    oeventControl.p rototype.neweve nt = function()
    {
    alert("new event attached");
    }
    oeventControl.p rototype.aevent = function(sColor )
    { // this button check using alert below
    alert(this.butt on.outerHTML);

    var self = this;

    // self.newevent point to correct function
    // self.newevent() ;

    //
    // why the code below does no attach the event and
    // the color of the button does not change
    this.button.onc lick=function() {self.newevent( );}
    this.button.sty le.color=sColor ;
    }

    // end control
    var oc;
    function initPage(){
    oc = new oeventControl() ;
    oc.paint(docume nt.all.controlP aintAear);

    }
    function aEvent(){
    oc.aevent("gree n");



    }
    </script>
    <body onload="initPag e()">
    <div id="controlPain tAear"></DIV>
    <BR/>
    <BR/>

    <INPUT TYPE="BUTTON" VALUE="Attach Event to the Control Button"
    onclick="aEvent ();">
    </body>

  • VK

    #2
    Re: Javascript Object Attach event not working


    bhatiaajay@gmai l.com wrote:[color=blue]
    > I am new to JavaScript objects. The object creates a button with no
    > events attached. Later I want to attach an event to the button created
    > by this object. The event attaches a function in the object.
    >
    > The code is listed below but the event is never attached.
    >
    > The 'this' reference for the button seem to be correct and the
    > 'self'
    > reference to the control(object) also seems to be correct.
    >
    > What am I doing wrong?
    >
    >
    > <html>
    > <script>
    >
    > // control
    > function oeventControl() {}
    >
    > oeventControl.p rototype.paint = function(canvas ){
    > this.mainBody = document.create Element("div");
    >
    > this.mainBody.a ppendChild(docu ment.createElem ent("input"));
    > this.button = this.mainBody.g etElementsByTag Name("input")[0];
    > this.button.set Attribute("TYPE ","BUTTON") ;
    > this.button.set Attribute("VALU E","click me after attaching
    > event");
    > this.button.sty le.color="red";
    >
    > canvas.innerHTM L= this.mainBody.o uterHTML;
    >
    > }
    >
    > oeventControl.p rototype.neweve nt = function()
    > {
    > alert("new event attached");
    > }
    > oeventControl.p rototype.aevent = function(sColor )
    > { // this button check using alert below
    > alert(this.butt on.outerHTML);
    >
    > var self = this;
    >
    > // self.newevent point to correct function
    > // self.newevent() ;
    >
    > //
    > // why the code below does no attach the event and
    > // the color of the button does not change
    > this.button.onc lick=function() {self.newevent( );}
    > this.button.sty le.color=sColor ;
    > }
    >
    > // end control
    > var oc;
    > function initPage(){
    > oc = new oeventControl() ;
    > oc.paint(docume nt.all.controlP aintAear);
    >
    > }
    > function aEvent(){
    > oc.aevent("gree n");
    >
    >
    >
    > }
    > </script>
    > <body onload="initPag e()">
    > <div id="controlPain tAear"></DIV>
    > <BR/>
    > <BR/>
    >
    > <INPUT TYPE="BUTTON" VALUE="Attach Event to the Control Button"
    > onclick="aEvent ();">
    > </body>[/color]

    The posted code gives you "Object expected" error on load.
    Could you post a working example of your problem?

    Comment

    • bhatiaajay@gmail.com

      #3
      Re: Javascript Object Attach event not working

      VK wrote:[color=blue]
      > The posted code gives you "Object expected" error on load.
      > Could you post a working example of your problem?[/color]

      VK
      the code is a working logic.
      In the line "click me after attaching event" , the word event gets
      wrapped to the next line and gives the error.

      line
      this.button.set Attribute("VALU E","click me after attaching event");

      with no wrapping should not give any error.

      Thanks for all your help.

      Comment

      • Michael Winter

        #4
        Re: Javascript Object Attach event not working

        On 22/10/2005 12:51, bhatiaajay@gmai l.com wrote:

        [snip]
        [color=blue]
        > What am I doing wrong?[/color]

        You're using a rather bizarre mix of DOM methods and proprietary IE
        features. IE's behaviour is rather odd, but it can be avoided very
        simply by going about each step rather more consistently. If you have
        any intention of supporting other browsers - arguably a necessity if
        you're working on something that will be public - changes will be essential.

        [snip]
        [color=blue]
        > this.mainBody.a ppendChild(docu ment.createElem ent("input"));
        > this.button = this.mainBody.g etElementsByTag Name("input")[0];[/color]

        Why didn't you just save a reference to the INPUT element in the first
        place?
        [color=blue]
        > this.button.set Attribute("TYPE ","BUTTON") ;
        > this.button.set Attribute("VALU E","click me after attaching event");[/color]

        Attributes should be set before adding a newly-created element to any
        document fragment, especially the type attribute (which is write-once).
        It's also best to avoid the *Attribute methods as IE doesn't always
        handle them well.

        [snip]
        [color=blue]
        > canvas.innerHTM L= this.mainBody.o uterHTML;[/color]

        Why not use the appendChild method?

        oeventControl.p rototype.paint = function(canvas ) {
        var mainBody = this.mainBody
        = document.create Element('div'),
        button = this.button
        = document.create Element('input' );

        button.type = 'button';
        button.value = 'Test';
        button.style.co lor = 'red';

        mainBody.append Child(button);
        canvas.appendCh ild(mainBody);
        };

        [snip]
        [color=blue]
        > oc.paint(docume nt.all.controlP aintAear);[/color]

        Use the getElementById method to obtain element references:

        oc.paint(docume nt.getElementBy Id('controlPain tAear'));

        The rest will work as-is. However, there are still issues with this code.

        There is no feature detection. This may lead to errors and
        uncontrollable failure in some browsers. How to rectify that would
        depend on what you're actually trying to do.

        The event listener you add dynamically to the INPUT element will cause a
        memory leak. See
        <URL:http://www.jibbering.c om/faq/faq_notes/closures.html#c lMem>.

        Mike

        --
        Michael Winter
        Prefix subject with [News] before replying by e-mail.

        Comment

        • bhatiaajay@gmail.com

          #5
          Re: Javascript Object Attach event not working

          Thanks Mike
          I did the changes as recommended and the code works fine now.

          Comment

          Working...