Jump to content

[SOLVED] call to javascript function not working - HELP!!


barbs75

Recommended Posts

Hey guys,

 

I have a javascript function which is cloning fields on a php form that i have on my site, this function is as follows:

 

<script type="text/javascript">
/*<![CDATA[*/
function create_fields(nr,roomType){
alert(roomType);
var root=document.getElementById(roomType);
var allB=[], allD=root.getElementsByTagName('div'), i=0, d;
// set the collection of the elements with className=inputfield
while(d=allD[i++]){
	d.className=='inputfield'?allB[allB.length]=d:null;
}
//removes all the elements, except the first
for(var j=allB.length-1;j>=1;j--){
	allB[j].parentNode.removeChild(allB[j]);
}
if(nr==1){return}//stops if a single bedroom is selected 
var clone;//the clone
var tags={'label':['for'],'input':['id','name'],'textarea':['id','name'],'p':[null]}
var obj, o, m, a, c;
for(var k=2;k<nr+1;k++){
	clone=allB[0].cloneNode(true);
	for(p in tags){
	obj=clone.getElementsByTagName(p);m=0;
	while(o=obj[m++]){
		for(var q=0;q<tags[p].length;q++){
			if(p=='p'&&o.className=='largeblack'){
				c=o.getElementsByTagName('strong')[0].firstChild;
				c.nodeValue=c.nodeValue.replace(/_1/,'_'+k);
			}
			else if(p!='p'){
				a=o.getAttribute(tags[p][q]);
				o.setAttribute(tags[p][q],a.replace(/1/,k));
			}
		}
	}
	}
	root.appendChild(clone);
}
}
/*]]>*/
</script>

 

What i am trying to do is call this function within my page, which is shown below:

<SCRIPT LANGUAGE='javascript'>
create_fields('3','bedrooms');
</SCRIPT>

 

What the function does is clone my bedroom field, the code of which is shown below:

<div class="inputfield">
                        <p class="largeblack"><strong>Bedroom_1</strong></p>
                            <div class="dimensions">
                            	<label class="room" for="bedrooms[1][]">Dimensions:<br/>
                                	<span class="textSmallGreen">(in feet)</span>
                                </label>
                            	<input class="open" name="bedrooms[1][]" id="bedrooms[1][]" type="text" size="5" value="" /> x <input class="open" name="bedrooms[1][]" id="bedrooms[1][]" type="text" size="5" value="" />
                            </div>
                            <div class="descriptionHolder">
                            	<label class="room" style="width:90px;" for="bedrooms[1][]">Description:<br/>
                                	<span class="textSmallGreen">(max. 100 words)</span>
                                </label>
                            	<textarea class="open" name="bedrooms[1][]" id="bedrooms[1][]" size="100" cols="35" rows="5">Enter your description here.....
                            	</textarea>
                            </div>	
                       </div>
                       </div>

 

It then finds the 'bedrooms' div and places the cloned html into that div. When the script is run though, it doesn't do anything. I have placed alerts within the javascript function to test if it is actually calling the function, and it is.

 

I really can't figure out where i am going wrong, can anyone help me out? see where i could be going wrong?

 

cheers guys, appreciate it

 

Craig

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.