Results 1 to 3 of 3

Thread: DOM insertBefore problem

  1. #1
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default DOM insertBefore problem

    javascript
    Code:
    /**
    * dynamically add form fields to a form
    * you must pass in the id of the template fields and the id of the span where
    * the fields will be placed(before) and the name of the counter.  You name your
    * template fields starting with 0, the dynamic fields are incremented from 1.
    */
    
    var counters = new Array();
    
    function addFormFields(dynamicFormReadId, dynamicFormWriteId, counter, counterStart, url)
    {
    	if(!counters[counter])
    		counters[counter] = counterStart;
    	
    	counters[counter]++;
    
    	var newFields = document.getElementById(dynamicFormReadId).cloneNode(true);
    
    	newFields.id = '';
    	newFields.style.display = 'block';
    	
    	var newField = newFields.childNodes;
    
    	for (var i=0;i<newField.length;i++)
    	{
    		var theName  = newField[i].name;
    
    		if (theName)
    		{
    			newField[i].name = theName.substr(0, theName.length - 1) + counters[counter];
    			newField[i].value = '';
    		}
    
    		var theId  = newField[i].id;
    
    		if (theId)
    			newField[i].id = theName.substr(0, theName.length - 1) + counters[counter];
    	}
    
    	var insertHere = document.getElementById(dynamicFormWriteId);
    ln168	insertHere.parentNode.insertBefore(newFields,insertHere);
    }
    html
    Code:
    		<div id="meeting0">
    			<input type="text" name="m_name0" value="">
    			<input type="text" name="m_date0" value="">
    			<input type="text" name="m_hours0" value="" size="5" onchange="updateHours(['tot_m_hours','total_hours'],this.value);return false;"> hours
    		</div>
    		<span class="meetingHolder"><!-- USED AS PLACEHOLDER FOR ADDING MEETING HOURS --></span>
    		<input type="button" class="addButton" onclick="addFormFields('meeting0','meetingHolder','m0',0, ''); return false" value="Add Meeting Hours">

    giving me the error
    Code:
    insertHere has no properties line 168
    Edit: .....

    what the script is attempting to perform is clone the "meeting" div id, and insert it above the span meetingHolder tag.
    This script is working for another portion of the site, however I cannot figure why it breaks on this template when they are represented the same way.

    other template
    Code:
          <div id="dynamicFormReadLang">
            <label>Related Doc Id:</label>
            <input type="text" name="relatedDoc0" id="langrelatedDoc0" value="{top.RELATED_DOC0}" class="field" />
            <a href="/admin/searchDocId.php?relatedDocNum=langrelatedDoc0" target="newWin" onclick="popUpWin(this.href,'console',506,465);return false;">Search for Document Id</a>
            <br />
          </div>
          <span id="dynamicFormWriteLang"><!-- USED AS PLACEHOLDER TO INSERT DYNAMIC FIELDS --></span>
          <p class="submit">
            <input type="button" value="Add More Related Doc Fields" onclick="addFormFields('dynamicFormReadLang', 'dynamicFormWriteLang', 'c1', {top.RELATED_DOC_COUNT}, '/admin/searchDocId.php?relatedDocNum=langrelatedDoc')" />
          </p>
    Last edited by boogyman; 01-09-2008 at 10:01 PM.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    <span class="meetingHolder">
    var insertHere = document.getElementById(dynamicFormWriteId);
    Are we noticing a slight problem here?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    shoot me now? haha I cannot believe it was something that stupid.... oh well, 2.5 days of wasted time n effort over 5 characters... Thanks Twey

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •