Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery change doesn't work on select

Why doesn't this work? I'm not sure what I'm doing wrong. I'm certain that jquery is working on the page.

Can anyone help me? Thanks in advance

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="styles/derweco.css"/>
    <script type="text/javascript" src="scripts/jquery-2.1.0.js"></script>
    <script type="text/javascript">

        $('#numbers').change(function(){
            alert('other value');
        });
    </script>
</head>
<body>
<div id="home">
    <select id="numbers">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</div>
</body>
</html>
like image 848
user1345883 Avatar asked Mar 23 '14 13:03

user1345883


1 Answers

You need to wrap your code inside DOM ready handler $(document).ready(function() {...}); or shorter form $(function() {...}); to make sure all of your DOM elements have been loaded properly before executing your jQuery code.

$(function() {
    $('#numbers').change(function(){
        alert('other value');
    });
});

Fiddle Demo

like image 91
Felix Avatar answered Sep 28 '22 01:09

Felix