Forum Moderators: open

Message Too Old, No Replies

IE6 + removeChild() not working

         

redwax

7:02 pm on Jun 17, 2010 (gmt 0)

10+ Year Member



Hi

I have a script which duplicates an element on click (and does some cookie creating, id renaming etc) and then removes the element on a second click. It is for adding items to a shortlist (the cookies get read on pae reload and the list is handles by php from their, before being saved into the db later).

It works fine in FF, Chrome, IE8 but fails in IE6 when invoking the removeChild(); method. Simply put, nothing happens. The script is as follows:

<script>

function mkCooki(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function ersCooki(name) {
mkCooki(name,"",-1);
}

function shortlist(srcElem, targCnt) {
var srcEl = document.getElementById (srcElem);//select the element
var cnt = document.getElementById (targCnt);
var cookVal = srcElem.slice(5);
var updSrcBtn = srcEl.getElementsByTagName("ul")[0].getElementsByTagName("li")[1].firstChild;//find the add/remove button
if (updSrcBtn.firstChild.nodeValue=='Remove'){
updSrcBtn.setAttribute("onclick", "shortlist('"+srcElem+"', '"+targCnt+"');");
updSrcBtn.firstChild.nodeValue="Add";
var remEl = document.getElementById(srcElem+'-sl');
cnt.removeChild (remEl);
ersCooki('slist');
}else{
updSrcBtn.setAttribute("onclick", "shortlist('"+srcElem+"', '"+targCnt+"');");
updSrcBtn.firstChild.nodeValue="Remove";
var clonedEl = srcEl.cloneNode (true);
clonedEl.setAttribute("id", srcElem+'-sl');
cnt.appendChild (clonedEl);
mkCooki('slist',cookVal,10)
}


}
</script>


The cloning of the node works, together with the other actions nested within the 'else' clause, but the actions contained within the initial 'if' (the 'if (updSrcBtn.firstChild.nodeValue=='Remove')') don't function at all in IE6.

I read a post here: [highdots.com ] and tried playing around with the suggestions made but to no avail.

I'd obviously love to ditch support for IE6 but unfortunately this isn't an option yet.

Any assistance would be grately apreciated as I rarely use javascript and so struggle a little.

Thanks

redwax

9:14 pm on Jun 21, 2010 (gmt 0)

10+ Year Member



I should say that I have tracked down where this issue appears to be. and it occurs in ie + chrome, but not Firefox. I can see the error thrown in chrome as 'getElementsByTagName' of null on the line where var updSrcBtn is set up.

the function as it stands is:

function shortlist(srcElem, targCnt, doremove) {
var srcEl = document.getElementById(srcElem);//select the element
var cnt = document.getElementById(targCnt);
var count = document.getElementById('slCount').firstChild;
var cookVal = srcElem.slice(4);
var updSrcBtn = srcEl.getElementsByTagName("ul")[0].getElementsByTagName("li")[1].firstChild;//find the add/remove button
if (updSrcBtn.firstChild.nodeValue=='Take off Shortlist' || doremove == true){
updSrcBtn.setAttribute("onclick", "shortlist('"+srcElem+"', '"+targCnt+"');");
updSrcBtn.firstChild.nodeValue="Add to Shortlist";
var remEl = document.getElementById(srcElem+'-sl');
cnt.removeChild (remEl);
ersCooki('favProp['+cookVal+']');
count.nodeValue--;
}else{
updSrcBtn.setAttribute("onclick", "shortlist('"+srcElem+"', '"+targCnt+"');");
updSrcBtn.firstChild.nodeValue="Take off Shortlist";
var clonedEl = srcEl.cloneNode (true);
clonedEl.setAttribute("id", srcElem+'-sl');
cnt.appendChild (clonedEl);
mkCooki('favProp['+cookVal+']',cookVal,10);
count.nodeValue++;
}


The appending part of the function works fine, but the removing part doesn't.

live version can be seen here: <snip>

I would be really grateful for any help :)

[edited by: bill at 10:16 pm (utc) on Jun 22, 2010]
[edit reason] No links to your sites or examples please [/edit]