"parentObj is not defined" error when removing created elements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newholborn
    New Member
    • Aug 2007
    • 12

    "parentObj is not defined" error when removing created elements

    Hi everyone. I have some questions, which should be rather easy to reply, but as I am working on PHP, JavaScript, XML, CSS, Photoshop and other stuff at the same time, my head, which is about to explode, would be grateful for any answers :).

    1) I have created a simple navigation system which loads local html documents on a main big DIV element in my page. Although greek language (iso-8859-7) is working outside that DIV, inside it i see question marks. My HTML files only contain <div>..More elements..</div>. I have tried calling complete HTML documents (with html, head, body etc elements) but no luck. Is that approach wrong? Any ideas why this happens?

    2) I use JavaScript to dynamically generate gadgets on the sidebar of my page with the document.create Element() and SomeElement.app endChild() methods. Whatever element wasn't originally on the page though (the divs I create), gives a "parentObj is not defined" error when i remove it, but the result is ok. It seems like the document element content needs to be copied to the browser memory and be refreshed...

    [CODE=javascript]function addSidebarEleme nt() {
    var ni = document.getEle mentById('gadge ts');
    var numi = document.getEle mentById('theVa lue');
    var num = (document.getEl ementById('theV alue').value -1)+ 2;
    numi.value = num;
    var newdiv = document.create Element('div');
    var divIdName = 'newgadget'+num ;
    newdiv.setAttri bute('id',divId Name);
    newdiv.setAttri bute('style','d isplay: none;');
    newdiv.innerHTM L="<h2 onclick=\"Effec t.toggle('"+div IdName+"Content ','blind')\">"+ divIdName+"</h2><ul id=\""+divIdNam e+"Content\"><l i><a href=\"#\">Fusc e dui neque fringilla</a></li></ul><span onclick=\"remov eElement(this); \">Remove Gadget</span>";
    ni.appendChild( newdiv);

    Effect.SlideDow n(divIdName);
    }

    function removeElement(d ivObj) {
    var parentObj = document.getEle mentById('gadge ts');
    var childObj=divObj .parentNode;
    var divId=childObj. getAttribute('i d');
    Effect.SlideUp( divId);

    setTimeout("par entObj.removeCh ild(childObj)", 2000);
    }[/CODE]

    Any explanations? I am worried that this problem is gonna make it worse after I add more stuff dynamically...
    Last edited by gits; Sep 12 '07, 05:14 PM. Reason: fix code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Changed the thread title to something more meaningful.

    Does this error occur on the setTimeout?

    Comment

    • newholborn
      New Member
      • Aug 2007
      • 12

      #3
      Probably not because it was the same before I added that line. I think it's line 17. It seems that the second function can't call any element by it's id. :/

      And I can't change the topic title. (???)

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Look at your modified Code.
        I did it.

        [CODE=javascript]
        var parentObj = null; //Defined here.
        function addSidebarEleme nt() {
        var ni = document.getEle mentById('gadge ts');
        var numi = document.getEle mentById('theVa lue');
        var num = (document.getEl ementById('theV alue').value -1)+ 2;
        numi.value = num;
        var newdiv = document.create Element('div');
        var divIdName = 'newgadget'+num ;
        newdiv.setAttri bute('id',divId Name);
        newdiv.setAttri bute('style','d isplay: none;');
        newdiv.innerHTM L="<h2 onclick=\"Effec t.toggle('"+div IdName+"Content ','blind')\">"+ divIdName+"</h2><ul id=\""+divIdNam e+"Content\"><l i><a href=\"#\">Fusc e dui neque fringilla</a></li></ul><span onclick=\"remov eElement(this); \">Remove Gadget</span>";
        ni.appendChild( newdiv);

        Effect.SlideDow n(divIdName);
        }

        function removeElement(d ivObj) {
        parentObj = document.getEle mentById('gadge ts'); //Redefined here.
        var childObj=divObj .parentNode;
        var divId=childObj. getAttribute('i d');
        Effect.SlideUp( divId);

        setTimeout("par entObj.removeCh ild(childObj)", 2000);
        }[/CODE]

        Now test this, I think it will work.
        Good Luck.


        Kind regards,
        Dmjpro.

        Comment

        • newholborn
          New Member
          • Aug 2007
          • 12

          #5
          Thanks Dmjpro. Unfortunately, with this modification, the error remains, while the added element is not removed. :(

          Comment

          • newholborn
            New Member
            • Aug 2007
            • 12

            #6
            Found it. I removed the scriptaculous effect and now no error, but unfortunately no effect.

            The new code:

            [CODE=javascript]function removeElement(d ivObj) {
            var parentObj = document.getEle mentById('gadge ts');
            var childObj=divObj .parentNode;
            var divId=childObj. getAttribute('i d');
            //Effect.SlideUp( divId);
            parentObj.remov eChild(childObj );
            //setTimeout("par entObj.removeCh ild(childObj)", 2000);
            }[/CODE]

            I'll keep you posted on the progress...

            By the way, whats the difference of creating a scriptaculous effect with

            [CODE=javascript]new Effect.EffectNa me('divid');[/CODE]

            and

            [CODE=javascript]Effect.EffectNa me('divid');[/CODE]???

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              You can do one more thing with my Code.
              As parentObj defined outside you can define childObj as well.

              [code=javascript]
              var parentObj = null; //Defined here.
              var childObj = null; //Defined here.
              function addSidebarEleme nt() {
              var ni = document.getEle mentById('gadge ts');
              var numi = document.getEle mentById('theVa lue');
              var num = (document.getEl ementById('theV alue').value -1)+ 2;
              numi.value = num;
              var newdiv = document.create Element('div');
              var divIdName = 'newgadget'+num ;
              newdiv.setAttri bute('id',divId Name);
              newdiv.setAttri bute('style','d isplay: none;');
              newdiv.innerHTM L="<h2 onclick=\"Effec t.toggle('"+div IdName+"Content ','blind')\">"+ divIdName+"</h2><ul id=\""+divIdNam e+"Content\"><l i><a href=\"#\">Fusc e dui neque fringilla</a></li></ul><span onclick=\"remov eElement(this); \">Remove Gadget</span>";
              ni.appendChild( newdiv);

              Effect.SlideDow n(divIdName);
              }

              function removeElement(d ivObj) {
              parentObj = document.getEle mentById('gadge ts'); //Redefined here.
              childObj=divObj .parentNode;
              var divId=childObj. getAttribute('i d');
              Effect.SlideUp( divId);

              setTimeout("par entObj.removeCh ild(childObj)", 2000);
              }[/code]

              Good Luck.

              Kind regards,
              Dmjpro.

              Comment

              Working...