0

In IE9 I am getting an error Microsoft JScript runtime error: 'populatedropdown' is undefined on the line <input type="button"....

I assume I am not escaping the literal values correctly. How should I change?

<html>
<head>
<title>Add items to dropdown</title>

<script type="text/javascript">
function populatedropdown(var devicelist) {
    var devList = document.frm.optdevices;
    var arrDev = split(devicelist, ";");

     devList.options.length = 0; // this removes existing options

     for (var i = 0; i <= 3; i++) {
         var option = new Option(arrDev[i],i);
         devList.options[i] = option; 
     }
}

</script>

</head>
<body>
<form name="frm">

<select name="optdevices">
</select>

<input type="button" value="TestAdd" onclick="populatedropdown('201;202;203;')" />

</form>
</body>
</html>
1
  • 3
    Unexpected token var on the first line of script. Also, separate your JavaScript from your HTML (attach event listeners on page load). Commented Nov 11, 2012 at 18:18

3 Answers 3

1

Separate the JavaScript from HTML and remove syntax error. See this fiddle

function populatedropdown(devicelist) {
    var devList = document.frm.optdevices;
    var arrDev = devicelist.split(';'); // split not defined

     devList.options.length = 0; // this removes existing options

     for (var i = 0; i <= 3; i++) {
         var option = new Option(arrDev[i],i);
         devList.options[i] = option; 
     }
}

// on window load
window.addEventListener('load', function load(){
    // attach event listener to button
    document.getElementById('testID').addEventListener('click', function click(){
        populatedropdown('201;202;203;');
    }, false);
}, false);
​

HTML

<form name="frm">

<select name="optdevices">
</select>

<input id="testID" type="button" value="TestAdd" />

</form>​
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Fiddle will be really useful.
0

First, you should not write var via defining function, and second use of function split is wrong, split should be used like var result = strToSplit.split(";"), if you wanna split by ; , you can read about split here Javascript Split method

here is working snippet

function populatedropdown(devicelist) {
    var devList = document.frm.optdevices;
    var arrDev = devicelist.split(";");

     devList.options.length = 0; // this removes existing options

     for (var i = 0; i <= 3; i++) {
         var option = new Option(arrDev[i],i);
         devList.options[i] = option; 
     }
}

just tested on IE9

2 Comments

Ah I assumed it was the html. Very new to JavaScript.
@user619818, website - w3schools.com, that I have mentioned, is a pretty good website for newbies, I have begun with it by myself
0

Your function is not valid as you have var in the function parameter. As long as you have syntax error in your script, the function won't be recognized. function populatedropdown(var devicelist) is not valid. There should not be var in the parameter

function populatedropdown(devicelist) {
    var devList = document.frm.optdevices;
    var arrDev = split(devicelist, ";");

     devList.options.length = 0; // this removes existing options

     for (var i = 0; i <= 3; i++) {
         var option = new Option(arrDev[i],i);
         devList.options[i] = option; 
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.