SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: No parentNode?
-
Aug 4, 2007, 01:51 #1
- 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){
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'); } } } }
Code:<dd> <input id="c-name" class="text" type="text" value="" name="c-name"/> </dd>
-
Aug 4, 2007, 03:14 #2
- 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));
-
Aug 4, 2007, 03:33 #3
- 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