473,388 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

"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.createElement() and SomeElement.appendChild() 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...

Expand|Select|Wrap|Line Numbers
  1. function addSidebarElement() {
  2.   var ni = document.getElementById('gadgets');
  3.   var numi = document.getElementById('theValue');
  4.   var num = (document.getElementById('theValue').value -1)+ 2;
  5.   numi.value = num;
  6.   var newdiv = document.createElement('div');
  7.   var divIdName = 'newgadget'+num;
  8.   newdiv.setAttribute('id',divIdName);
  9.   newdiv.setAttribute('style','display: none;');
  10.   newdiv.innerHTML="<h2 onclick=\"Effect.toggle('"+divIdName+"Content','blind')\">"+divIdName+"</h2><ul id=\""+divIdName+"Content\"><li><a href=\"#\">Fusce dui neque fringilla</a></li></ul><span onclick=\"removeElement(this);\">Remove Gadget</span>";
  11.   ni.appendChild(newdiv);
  12.  
  13.   Effect.SlideDown(divIdName);
  14. }
  15.  
  16. function removeElement(divObj) {
  17. var parentObj = document.getElementById('gadgets');
  18. var childObj=divObj.parentNode;
  19. var divId=childObj.getAttribute('id');
  20. Effect.SlideUp(divId);
  21.  
  22. setTimeout("parentObj.removeChild(childObj)",2000);
  23. }
Any explanations? I am worried that this problem is gonna make it worse after I add more stuff dynamically...
Sep 12 '07 #1
6 2528
acoder
16,027 Expert Mod 8TB
Changed the thread title to something more meaningful.

Does this error occur on the setTimeout?
Sep 13 '07 #2
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. (???)
Sep 13 '07 #3
dmjpro
2,476 2GB
Look at your modified Code.
I did it.

Expand|Select|Wrap|Line Numbers
  1. var parentObj = null; //Defined here.
  2. function addSidebarElement() {
  3.   var ni = document.getElementById('gadgets');
  4.   var numi = document.getElementById('theValue');
  5.   var num = (document.getElementById('theValue').value -1)+ 2;
  6.   numi.value = num;
  7.   var newdiv = document.createElement('div');
  8.   var divIdName = 'newgadget'+num;
  9.   newdiv.setAttribute('id',divIdName);
  10.   newdiv.setAttribute('style','display: none;');
  11.   newdiv.innerHTML="<h2 onclick=\"Effect.toggle('"+divIdName+"Content','blind')\">"+divIdName+"</h2><ul id=\""+divIdName+"Content\"><li><a href=\"#\">Fusce dui neque fringilla</a></li></ul><span onclick=\"removeElement(this);\">Remove Gadget</span>";
  12.   ni.appendChild(newdiv);
  13.  
  14.   Effect.SlideDown(divIdName);
  15. }
  16.  
  17. function removeElement(divObj) {
  18. parentObj = document.getElementById('gadgets'); //Redefined here.
  19. var childObj=divObj.parentNode;
  20. var divId=childObj.getAttribute('id');
  21. Effect.SlideUp(divId);
  22.  
  23. setTimeout("parentObj.removeChild(childObj)",2000);
  24. }
Now test this, I think it will work.
Good Luck.


Kind regards,
Dmjpro.
Sep 13 '07 #4
Thanks Dmjpro. Unfortunately, with this modification, the error remains, while the added element is not removed. :(
Sep 13 '07 #5
Found it. I removed the scriptaculous effect and now no error, but unfortunately no effect.

The new code:

Expand|Select|Wrap|Line Numbers
  1. function removeElement(divObj) {
  2. var parentObj = document.getElementById('gadgets');
  3. var childObj=divObj.parentNode;
  4. var divId=childObj.getAttribute('id');
  5. //Effect.SlideUp(divId);
  6. parentObj.removeChild(childObj);
  7. //setTimeout("parentObj.removeChild(childObj)",2000);
  8. }
I'll keep you posted on the progress...

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

Expand|Select|Wrap|Line Numbers
  1. new Effect.EffectName('divid');
and

Expand|Select|Wrap|Line Numbers
  1. Effect.EffectName('divid');
???
Sep 13 '07 #6
dmjpro
2,476 2GB
You can do one more thing with my Code.
As parentObj defined outside you can define childObj as well.

Expand|Select|Wrap|Line Numbers
  1. var parentObj = null; //Defined here.
  2. var childObj = null; //Defined here.
  3. function addSidebarElement() {
  4.   var ni = document.getElementById('gadgets');
  5.   var numi = document.getElementById('theValue');
  6.   var num = (document.getElementById('theValue').value -1)+ 2;
  7.   numi.value = num;
  8.   var newdiv = document.createElement('div');
  9.   var divIdName = 'newgadget'+num;
  10.   newdiv.setAttribute('id',divIdName);
  11.   newdiv.setAttribute('style','display: none;');
  12.   newdiv.innerHTML="<h2 onclick=\"Effect.toggle('"+divIdName+"Content','blind')\">"+divIdName+"</h2><ul id=\""+divIdName+"Content\"><li><a href=\"#\">Fusce dui neque fringilla</a></li></ul><span onclick=\"removeElement(this);\">Remove Gadget</span>";
  13.   ni.appendChild(newdiv);
  14.  
  15.   Effect.SlideDown(divIdName);
  16. }
  17.  
  18. function removeElement(divObj) {
  19. parentObj = document.getElementById('gadgets'); //Redefined here.
  20. childObj=divObj.parentNode;
  21. var divId=childObj.getAttribute('id');
  22. Effect.SlideUp(divId);
  23.  
  24. setTimeout("parentObj.removeChild(childObj)",2000);
  25. }
Good Luck.

Kind regards,
Dmjpro.
Sep 14 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Liang | last post by:
Hi, I use "defined $r_libs->{$name}" to check first if a key exists in a hash table. But Perl gives a warning WHENEVER the key exists: "Use of uninitialized value". Would u please help to...
4
by: Dmitry | last post by:
Hello, Sometimes on js code execution Mozilla 1.5 prints in JS concole the following error: "Error: fn is not defined" where "fn" is the function name surely defined _before_ the line that...
11
by: MLH | last post by:
I copied the following code snippet from A97 HELP. Am getting an error at compile time suggesting there's a problem with the first line (compile error, user-defined type not defined). It is likely...
6
by: david | last post by:
When I compile a window form client for web service, there is error message as follows: Program 'D:\usr\winVBcontrol\WinClient4WebServiceFileCS\obj\Debug\WinClient4WebServiceFileCS.exe' does not...
10
by: Eric G. Harrison | last post by:
We have a project with many other projects referenced (all of which are referenced at the project level and are included in the solution). Frequenly, if we make a change in project A (such as adding...
2
by: Yannick Turgeon | last post by:
Hello, I'm using A97 (french version) on XP (english version). Since this afternoon, all the Access built-in french-equivalent function are generating a "Sub or Function not defined" error. An...
2
by: Yarik | last post by:
Hello, I am not sure the subject of my post adequately describes the problem I am trying to solve, so I think a specific example would be helpful. Let's say there are XML descriptions of...
3
by: KelHemp | last post by:
I have compiled data from 13 tables, one for each month of the year that hold 45 fields each, and another for employee information, that holds 16 fields. The database is intended to record hours and...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.