0

So I am trying to make a simple dropdown menu that when a value is clicked a function is run, but I'm having some issues accessing the function.

When I run this fiddle (code below), I get a console message of 'made it'.

HTML

<div id="dropdown">
<select>
    <option>Near</option>
    <option onclick="console.log('made it');">Far</option>
    <option>Very Far</option>
    <option>Distant</option>
</select>
</div>

But if I try to move the message into a function like in this fiddle (code below), I get an error saying the function test is not defined.

HTML

<div id="dropdown">
<select>
    <option>Near</option>
    <option onclick="test();">Far</option>
    <option>Very Far</option>
    <option>Distant</option>
</select>
</div>

JavaScript

var test = function () {
    console.log("Made it");
};

Can someone explain why this is happening? Shouldn't my HTML onclick be able to find the test() function?

BDD
  • 665
  • 17
  • 31

0 Answers0