user3274696
user3274696

Reputation: 89

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

<section class="question-three">
                <h4>Question 3</h4>
                <ol>
                    <li>A partnership is a business entity 
                    <select id="selectionOne">
                        <option value="#">Please Select...</option>
                        <option value="sole trader">sole trader</option>
                        <option value="members">members</option>
                        <option value="shareholders">shareholders</option>
                        <option value="run">run</option>
                        <option value="limited company">limited company</option>
                        <option value="owned">owned</option>
                    </select>
                    by two ore more people who carry a business collectively with a view to making a profit.
                </li>

JavaScript

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

        if(check.options == dropDownList.answers[5]) {
            console.log('The answer is right!');
        } else {
            console.log('false')
        }
    }
};

dropDownList.checkAnswer();

Could anyone please help me?

Thank You

Upvotes: 0

Views: 77

Answers (1)

mplungjan
mplungjan

Reputation: 178350

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
}
<section class="question-three">
                <h4>Question 3</h4>
                <ol>
                    <li>A partnership is a business entity 
                    <select id="selectionOne">
                        <option value="#">Please Select...</option>
                        <option value="sole trader">sole trader</option>
                        <option value="members">members</option>
                        <option value="shareholders">shareholders</option>
                        <option value="run">run</option>
                        <option value="limited company">limited company</option>
                        <option value="owned">owned</option>
                    </select>
                    by two ore more people who carry a business collectively with a view to making a profit.
                </li>
<button id="checkIt">Check</button>

Upvotes: 1

Related Questions