Reputation: 2509
So I have a list, and I want to dynamically add an event via JavaScript. I have it working great in Firefox, but I also need it to work in IE6 (ugh), but it's required. It doesn't have to be pretty, just needs to work. The event that triggers simply removes the item from the list. I am not sure what I need to do to get it working. Here is small piece of what I have so far. The ids are unique, I just put that one in as an example. It works great in all newer browsers.
var id = "123456";
var list = document.createElement("ul");
var listElement = document.createElement("li");
listElement.setAttribute("id", id);
listElement.setAttribute("onclick", "removeFromList('" + id + "')");
listElement.appendChild(document.createTextNode(content));
list.appendChild(listElement);
document.getElementById('myElement').appendChild(list);
Upvotes: 0
Views: 2354
Reputation: 51817
i don't have an IE6 to test this, but replacing the onclick-line:
listElement.setAttribute("onclick", "removeFromList('" + id + "')");
with this might work:
listElement.onclick = function(){ removeFromList(id); };
you also could use attachEvent for IE and stick to you old solution (or better use addEventListener) on the newer ones.
Upvotes: 2
Reputation: 21527
Pure javascript, should work in IE6 :
var id = "123456";
var list = document.createElement("ul");
var listElement = document.createElement("li");
listElement.setAttribute("id", id);
listElement.appendChild(document.createTextNode(content));
list.appendChild(listElement);
document.getElementById('myElement').appendChild(list);
if( listElement.addEventListener ) {
listElement.addEventListener("click", function(e) {
removeFromList( id );}, false );
} else {
listElement.attachEvent( "onclick", function(e) {
removeFromList( id );});
}
Upvotes: 2