Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to change value of input box to selected option of comboBox

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading