0

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>
2
  • <select id="listname" onchange="showEmployee(this.value)"> <option value="choose" selected>choose employee</option> Commented Oct 5, 2013 at 3:02
  • Is it ok to use jQuery? or do you want Javascript only? Commented Oct 5, 2013 at 3:03

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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);
}

1 Comment

^Set addOption as the body's onload event handler.

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.