Rachel Metallo
Rachel Metallo

Reputation: 1

How do I alter this code to Instead code a javascript function?

How do I alter this code to Instead code a javascript function that is called from an onload event handler of the body tag. The function should use the methods of the dom for creating new "option" elements, properly configuring, and appending to the select element?

<select id="listname" onchange="showEmployee(this.value)">
<option value="choose" selected>choose employee</option>

Upvotes: 0

Views: 73

Answers (2)

user2422457
user2422457

Reputation:

function addOption(selectTagId) {
    var select = document.getElementById(selectTagId);

    var option = document.createElement("option");        
    option.value = "choose";
    option.appendChild(document.createTextNode("choose employee"));

    select.appendChild(option);
}

Upvotes: 0

bfavaretto
bfavaretto

Reputation: 71939

code a javascript function that is called from an onload event handler of the body tag

<script>
window.onload = function() {
    // code here
}
</script>

The function should use the methods of the dom for creating new "option" elements, properly configuring, and appending to the select element

var opt = new Option("option text", "val");
document.getElementById("listname").appendChild(opt);
// or in html5: document.getElementById("listname").add(opt);

Now all you have to do is put both together.

Upvotes: 1

Related Questions