Forum : Why is this dropdown script not working...
Brief description  about Online courses   join in Online courses
View Rammohan S Reddy 's Profile

Why is this dropdown script not working...

The below script is for dynamic dropdown. I tried with this, though the gets populated, only the last digit is shown in the options. Pls suggest on this.

<script type="text/javascript">
function dy(){
var sels=document.getElementById("sel");
var optn = new Option;
for (i=1; i<10; i ){
optn.text=i;
optn.value=i;
sels.options[i]= optn;
}}
</script>
</head>

<body onload="dy()">
<select id="sel">
</select>
</body>
Asked by Rammohan S Reddy | Oct 18, 2010 |  Reply now
Replies (2)
View rammohan s reddy 's Profile
Thanks
Oct 19, 2010
View teacher siliconindia 's Profile
Hi Rammohan,

check the below script, it may help u,


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title>Test Page</title>

<script type="text/javascript">

function doChange(srcE, targetId)

{

var val = srcE.options[srcE.selectedIndex].value;

var targetE = document.getElementById(targetId);

alert(val);

removeAll(targetE);

if(val == 'languages')

{

addOption('C++', targetE);

addOption('Java', targetE);

addOption('Scheme', targetE);

}

else if(val == 'tools')

{

addOption('Visual Studio', targetE);

addOption('Netbeans', targetE);

addOption('Eclipse', targetE);

}

}

function addOption(value, e)

{

var o = new Option(value);

try

{

e.add(o);

}

catch(ee)

{

e.add(o, null);

}


}



function removeAll(e)

{

for(var i = 0, limit = e.options.length; i < limit - 1; ++i)

{

e.remove(1);

}

}

</script>

</head>

<body>

<form action="#">

<select name="selOne" id="selOne" onchange="doChange(this, 'selTwo')">

<option value="default">---Select Something---</option>

<option value="languages">Languages</option>

<option value="tools">Tools</option>

</select>

<select name="selTwo" id="selTwo">

<option value="default">---Select Something---</option>

</select>

</form>

</body>

</html>

and check this link also,
http://www.felgall.com/jstip22.htm
Oct 18, 2010