SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: No parentNode?

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    antwerp
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    No parentNode?

    Hello,

    I recently started to dip my toes into the DOM and Javascript but I ran into a problem. I get this error:

    parent has no properties
    [Break on this error] if(parent.lastChild == targetElement){
    Here's the javascript:
    Code:
    function insertAfter(newElement, targetElement){
    	var parent = targetElement.parentNode;
    	if(parent.lastChild == targetElement){
    		parent.appendChild(newElement);
    	}else{
    		parent.insertBefore(newElement, targetElement.nextSibling);	
    	}
    }
    
    function contactCheck(){
    	if(!document.getElementById('contactform')) return false;
    	function okimg(where){
    		var okimg = document.createElement('img');
    		okimg.setAttribute('alt', 'Correct');
    		okimg.setAttribute('src', '/media/img/form_good.gif');
    		okimg.setAttribute('class', 'ok');
    		okimg.setAttribute('id', where + 'ok');
    		insertAfter(okimg, where);
    	}
    	if(document.getElementById('c-name')){
    		document.getElementById('c-name').onchange = function(){
    			if(document.getElementById('c-name').value != ''){
    				okimg('c-name');
    			}
    		}
    	}
    }
    The html involved
    Code:
    <dd>
    	<input id="c-name" class="text" type="text" value="" name="c-name"/>
    </dd>
    I'm kinda stuck on this, I don't understand how the dd can't be the parentNode?

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okimg('c-name');
    ==>
    insertAfter(okimg, 'c-name');
    ==>
    var parent = 'c-name'.parentNode;

    You need a document.getElementById() somewhere in the chain. Presumably by replacing insertAfter(okimg, where); with insertAfter(okimg, document.getElementById(where));

  3. #3
    SitePoint Member
    Join Date
    Sep 2004
    Location
    antwerp
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    I changed the insertAfter function to:

    Code:
    function insertAfter(newElement, targetElement){
    	targetElement = document.getElementById(targetElement);
    	var parent = targetElement.parentNode;
    	if(parent.lastChild == targetElement){
    		parent.appendChild(newElement);
    	}else{
    		parent.insertBefore(newElement, targetElement.nextSibling);	
    	}
    }


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
  •