?
Solved

Javascript array loop function not working

Posted on 2009-07-16
10
Medium Priority
?
375 Views
Last Modified: 2013-11-19
I have some areas on my page with tinyMCE editors. I need to loop through the areas and return some values in an array.

I have to perform a few function inside of the loop as well such as removing unneeded content and replacing some invalid characters.

Here is what I have. It is currently not working.

Thank you for your help!
function removeElement(parentElm, childDiv){
	if (document.getElementById(childDiv)) {     
		var child = document.getElementById(childDiv);
		var parent = document.getElementById(parentElm);
		parent.removeChild(child);
	}
}
 
function getElementsByClass(node,searchClass,tag) {
var classElements = new Array();
var els = node.getElementsByTagName(tag); // use "*" for all elements
var elsLen = els.length;
var pattern = new RegExp("\\b"+searchClass+"\\b");
for (i = 0, j = 0; i < elsLen; i++) {
 if ( pattern.test(els[i].className) ) {
 classElements[j] = els[i];
 j++;
 }
}
return classElements;
}
//this script gets the contents of the edit areas
function areaGetContents(){
	var i;
	var contentAreas = getElementsByClass(document,'contentArea','*');
	var fullContent = new Array();
	for (i=1; i<=contentAreas.length; i++) {
		 document.getElementById('editorArea_'+i).innerHTML = tinyMCE.get('editorArea_'+i).getContent();
		 removeElement('contentArea_'+i,'editorArea_'+i+'_parent');
		 fullContent[i] = contentAreas[i].innerHTML;
		 var divModStart = fullContent[i].replace(/<textarea style="display: none;"/g, '<div');
	 	 var divModEnd = divModStart.replace(/textarea>/g, 'div>');
 		 var ltMod = divModEnd.replace(/\&lt;/g, '<');
		 var gtMod = ltMod.replace(/\&gt;/g, '>');
		 fullContent[i] = gtMod;
	     alert(fullContent[i]);
	}
}

Open in new window

0
Comment
Question by:Shaye Larsen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 82

Accepted Solution

by:
hielo earned 1200 total points
ID: 24874375
>>for (i=1; i<=contentAreas.length; i++)
You probably meant:

"less than" NOT "less than or equal to". Also, arrays are 0-indexed:
for (i=0; i<contentAreas.length; i++)
0
 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24875124
K, I made changes to do that, but it still doesn't work.

I am getting the following error on line 5 that has

parent.removeChild(child);


error from firebug:

Error: uncaught exception: [Exception... "Node was not found"  code: "8" nsresult: "0x80530008 (NS_ERROR_DOM_NOT_FOUND_ERR)"  location: "http://fabuso/js/sitecreator_edit_page_scripts.js Line: 82"]
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 1200 total points
ID: 24875547
>>"Node was not found"
There's the reason. Within your loop you are doing:
removeElement('contentArea_'+i,'editorArea_'+i+'_parent');
 
So, when i=0, you are actually calling:
removeElement('contentArea_0','editorArea_0_parent');
 
So, do you have some element with id="contentArea_0" and also WITHIN that element you need some other element with id="editorArea_0_parent".
 
The same rationale applies when i=1, etc. I can't tell you more since you didn't post much. If you have a link to your page, please post it.

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24875840
Yeah, I have those elements with those IDs. Tomorrow I'll get in a make a test page, its on my local system.
0
 
LVL 12

Assisted Solution

by:Barry Jones
Barry Jones earned 300 total points
ID: 24879259
Are you sure that the child is a child of the parent that you are passing in?

Why not let the removeElement() function determine the parent?
removeElement = function(ele){
	var ele_ret = ele;
	if (ele && (ele.parentNode)){ele.parentNode.removeChild(ele);}
	return ele_ret;
}

Open in new window

0
 
LVL 12

Expert Comment

by:Barry Jones
ID: 24879271
Also, your removeElement() function expects the parent passed in first, and in your code it appears that you have reversed the order of the parameters:
removeElement('contentArea_'+i,'editorArea_'+i+'_parent');

Open in new window

0
 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24879400
Yeah, the naming convention would make it seem like so. tinyMCE produces a lot of markup when they convert your textareas into editors. Since I have multiple areas on one page it produces the topmost element for the new tinyMCE markup as editorArea_0_parent, editorArea_1_parent, editorArea_2_parent, etc. My elms that contain the tinyMCE editors all have a class of 'contentArea' and an id that is contentArea_0, contentArea_1, contentArea_2 respectively.
0
 
LVL 12

Expert Comment

by:Barry Jones
ID: 24879590
Ah ok...
0
 
LVL 1

Author Comment

by:Shaye Larsen
ID: 24981890
I resorted to using jQuery and that made everything simple and fixed whatever was going wrong. Thanks for you help!
0
 
LVL 1

Author Closing Comment

by:Shaye Larsen
ID: 31604423
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question