Forum Moderators: open

Message Too Old, No Replies

Event not fired in IE when option selected

         

cqhere

12:48 am on Jun 3, 2005 (gmt 0)



In IE, when I build a select using the DOM and then click on an item in the generated drop-down list, "onclick" does not fire. Works fine in FireFox.

Here is my code:

<html>
<head>

<script language="JavaScript" type="text/JavaScript">

function buildList() {
var span = document.getElementById("list");

// select
var select = document.createElement("select")
var selectAttribute = document.createAttribute("id");
selectAttribute.value = "list";
select.setAttributeNode(selectAttribute);
var selectAttribute = document.createAttribute("onchange");
selectAttribute.value = "alert('event fired');";
select.setAttributeNode(selectAttribute);

// options
var option = document.createElement("option")
selectAttribute = document.createAttribute("value");
selectAttribute.value = "a";
option.setAttributeNode(selectAttribute);
option.appendChild(document.createTextNode(0))
select.appendChild(option)

option = document.createElement("option")
selectAttribute = document.createAttribute("value");
selectAttribute.value = "b";
option.setAttributeNode(selectAttribute);
option.appendChild(document.createTextNode(1))

select.appendChild(option)
span.appendChild(select)
}
</script>
</head>

<body onload="buildList()">
<form name="myform" action="">
<span id="list"></span>
</form>
</body>
</html>

Thanks,
Cian

orion_rus

6:28 am on Jun 6, 2005 (gmt 0)

10+ Year Member



it's not working because setAttribute and other functions like this implemented only in Mozilla browsers, IE has only limited support.
You need to change all like this:
var option = document.createElement("option")
selectAttribute = document.createAttribute("value");
selectAttribute.value = "a";
to this:
option.value='a';
and all should work fine in both
Good luck to you!