Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling javascript function from a select onChange [duplicate]

So I have a simple HTML select box, and a javascript alert function. I want the select box to have an onchange event that will call the javascript alert function. This is what I have so far:

HTML

<div style="float: left">Type: <select id="selector" onChange="jsfunction()">
  <option value="Pyr">Pyramidally</option>
  <option value="Lin">In-Lines</option>
  <option value="Sym">Symmetrically</option>
  <option value="Nsym">Non-Symmetrically</option>
</select>
</div>

Javascript

function jsfunction(){
    alert("hi");
}

It doesn't work and for the life of me I can't figure out why. Most other questions of this nature I've found suggest "" around calling the function - which I've got or making the onChange 'c' capital, which I also have. Any possible help? I'd be very grateful.

like image 964
Genome314 Avatar asked Jul 15 '14 05:07

Genome314


1 Answers

<!DOCTYPE html>
<html>
    <head>
        <script>
            function jsfunction(){
                alert("hi");
            }
        </script>
    </head>
    <body>
        <select id="selector" onchange="jsfunction()">
            <option value="Pyr">Pyramidally</option>
            <option value="Lin">In-Lines</option>
            <option value="Sym">Symmetrically</option>
            <option value="Nsym">Non-Symmetrically</option>
        </select>
    </body>
</html>

It works. You made a mistake in onchange.

like image 125
user3218194 Avatar answered Oct 10 '22 11:10

user3218194