Hola wolf777
Este script solo lo he probado en IE6 y Mozilla 1.7, en teoria debe de funcionar en IE5+ y NS6+
Código PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Crear un select</title>
</head>
<body>
<div id="selectCountry"></div>
<a href="javascript:void(0)" onclick="create_select()">Crear select</a>
<script language=JavaScript type=text/javascript>
<!--
var country_v=['(Select your country)','Afghanistan','Albania','Algeria'];
function create_select(){
// comprobar que form1 no este creado
for(var ct1 = 0 ; ct1 < document.forms.length; ct1++){
if(document.forms[ct1].name == "form1"){ return false}
}
// Donde vamos a colocar el formulario
var div_select = document.getElementById("selectCountry")
// Creamos el objeto form
var form_node = document.createElement("form")
div_select.appendChild(form_node)
// añadimos el atributo name
form_node.setAttribute("name","form1")
form_node.setAttribute("action","form.php")
var select_node = document.createElement("select")
form_node.appendChild(select_node)
select_node.setAttribute("name","select1")
for(var ct = 0; ct < country_v.length; ct++){
var option_node = document.createElement("option")
select_node.appendChild(option_node)
option_node.setAttribute("value",country_v[ct])
var text_node = document.createTextNode(country_v[ct])
option_node.appendChild(text_node)
}
/* IE solo me ha permitido acceder a las propiedades
y valores del select creado mediante la sintaxis de
arrays
*/
for(var ct1 = 0 ; ct1 < document.forms.length; ct1++){
if(document.forms[ct1].name == "form1"){
for(var ct2 = 0; ct2 < document.forms[ct1].elements.length; ct2++){
if(document.forms[ct1].elements[ct2].name == "select1" ){
/*En teoria podria añadir el evento onchange con
select_node.setAttribute("onchange","show_selectedIndex()")
pero en IE no funciona
*/
document.forms[ct1].elements[ct2].onchange= show_selectedIndex
}
}
}
}
}
function show_selectedIndex(){
alert(this.options[this.selectedIndex].value)
}
//-->
</script>
</body>
</html>
Espero que te sirva, un saludo.