I’m following an introduction-course to HTML, in this particular demo I made a comboBox:
//Combobox to select a course
<div>
<select id="courses" multiple size="3" onchange="choiceMade(this.value);">
<option value="" selected>--Choose--</option>
<option value="Angular">Angular</option>
<option value="ReactJS">React</option>
<option value="VueJS">Vue</option>
<option value="JavaScript">JavaScript</option>
<option value="Python">Python</option>
</select>
</div>
The selected value should be printed in this input box using JavaScript:
//Inputbox where above selected course should be printed
<div>
<label class="standoutLabel">Favorite Course:</label>
<input type="text" id="courseName">
</div>
The course tells me the function to do this should be this:
//Function to print selected choice in inputbox
function choiceMade(choice) {
document.getElementByID("courseName").value = choice;
}
In the video it works fine, but for me… Nope…Nothing…Nada
How do I get this to work? What am I missing?
I tried adding a function call to get the value of choiceMade so it would input the choice in the <input>, but it just isn’t working.
>Solution :
There is a small error in the JavaScript code.
The correct method to get an element by its ID is getElementById (with a lowercase “d”), not getElementByID (with an uppercase “D”). Below is the corrected choiceMade function:
// Function to print the selected choice in the input box
function choiceMade(choice) {
document.getElementById("courseName").value = choice;
}
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById