Skip to content Skip to sidebar Skip to footer

Javascript Interacting With Options

I currently have a dropdown list... to finish blank sentences off. The problem I have is Uncaught TypeError: Cannot read property 'options' of null Here is a snippet of my HTML

Solution 1:

You are not accessing the value. We also do not know when you trigger the event. You need to assign it after the select has rendered:

var dropDownList = {
    answers: ['sole trader', 'members', 'shareholders', 'run', 'limited company', 'owned'],
    checkAnswer: function() {
        var check = document.getElementById('selectionOne');

        if(check.value == dropDownList.answers[5]) {
            console.log('The answer is right!');
        } else {
            console.log('false')
        }
    }
};
window.onload=function() {
  document.getElementById("checkIt").onclick=function() {
    dropDownList.checkAnswer();
  }
  // or document.getElementById("selectionOne").onchange
}
<sectionclass="question-three"><h4>Question 3</h4><ol><li>A partnership is a business entity 
                    <selectid="selectionOne"><optionvalue="#">Please Select...</option><optionvalue="sole trader">sole trader</option><optionvalue="members">members</option><optionvalue="shareholders">shareholders</option><optionvalue="run">run</option><optionvalue="limited company">limited company</option><optionvalue="owned">owned</option></select>
                    by two ore more people who carry a business collectively with a view to making a profit.
                </li><buttonid="checkIt">Check</button>

Post a Comment for "Javascript Interacting With Options"