rondog Posted August 24, 2010 Share Posted August 24, 2010 I am having a problem getting appendChild to work in IE6 and 7. It works fine in firefox, chrome, safari and surprisingly IE8, but not 6 or 7 var odd = true; function generatePlaylist(name,album,artist,source,thumbnail) { var parent = document.getElementById("player-playlist"); var el = document.createElement('span'); if (odd) { el.setAttribute('class','aSong odd'); odd = false; } else { el.setAttribute('class','aSong'); odd = true } el.setAttribute('data-name',name.replace(/'/g,'')); el.setAttribute('data-album',album.replace(/'/g,'')); el.setAttribute('data-artist',artist.replace(/'/g,'')); el.setAttribute('data-source',source.replace(/'/g,'')); el.setAttribute('data-thumbnail',thumbnail.replace(/'/g,'')); el.setAttribute('onclick','test(this)'); el.innerHTML = name.replace(/'/g,''); parent.appendChild(el); //console.log(name + "\n" + album + "\n" + artist + "\n" + source + "\n" + thumbnail); } function test(who) { alert(who.getAttribute('data-source')); } any ideas? Link to comment https://forums.phpfreaks.com/topic/211653-appendchild-not-working-in-ie6-or-7/ Share on other sites More sharing options...
rondog Posted August 24, 2010 Author Share Posted August 24, 2010 rather than using setAttribute I just concatonated the string which seems to work. var odd = true; var appendMe = ""; function generatePlaylist(name,album,artist,source,thumbnail) { var parent = document.getElementById("player-playlist"); if (odd) { appendMe += "<span class='aSong odd' data-name='" + name.replace(/'/g,'') + "' data-album='" + album.replace(/'/g,'') + "' data-artist='" + artist.replace(/'/g,'') + "' data-source='" + source.replace(/'/g,'') + "' data-thumbnail='" + thumbnail.replace(/'/g,'') + "' onclick='test(this)'>" + name.replace(/'/g,'') + "</span>"; odd = false; } else { appendMe += "<span class='aSong' data-name='" + name.replace(/'/g,'') + "' data-album='" + album.replace(/'/g,'') + "' data-artist='" + artist.replace(/'/g,'') + "' data-source='" + source.replace(/'/g,'') + "' data-thumbnail='" + thumbnail.replace(/'/g,'') + "' onclick='test(this)'>" + name.replace(/'/g,'') + "</span>"; odd = true } parent.innerHTML = appendMe; } function test(who) { alert(who.getAttribute('data-source')); } I am still looking for the answer if anyone knows.. Link to comment https://forums.phpfreaks.com/topic/211653-appendchild-not-working-in-ie6-or-7/#findComment-1103380 Share on other sites More sharing options...
Adam Posted August 25, 2010 Share Posted August 25, 2010 I imagine IE6/7 are trying to use the parent property, as opposed to your defined variable. Just to be safe you should never name variables after any reserved keywords/properties. Link to comment https://forums.phpfreaks.com/topic/211653-appendchild-not-working-in-ie6-or-7/#findComment-1103512 Share on other sites More sharing options...
rondog Posted August 25, 2010 Author Share Posted August 25, 2010 the problem was using setAttribute with the onclick event. If I remove it, it works. Link to comment https://forums.phpfreaks.com/topic/211653-appendchild-not-working-in-ie6-or-7/#findComment-1103642 Share on other sites More sharing options...
Adam Posted August 26, 2010 Share Posted August 26, 2010 Ah yes of course. Technically events aren't actually attributes; in HTML you're just able to assign them like that. Try this: el.onclick = function() { test(this); } Link to comment https://forums.phpfreaks.com/topic/211653-appendchild-not-working-in-ie6-or-7/#findComment-1103868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.