Search code examples
jqueryselecthandler

jquery does not react on select/option change event


Can someone please tell me where the mistake is, I just can't see it. 'e1 has changed' should be logged to the console when the select changes.

html:

<select name="e1" id="e1">
    <option value="1">1</option>
    <option value="2">2</option>
</select>

js:

$('#e1').change(function() {
    console.log('e1 has changed');
});

jquery is definitely working since I'm successfully "getting" server data for other elements.


Solution

  • This code

    $('#e1').change(function() {
        console.log('e1 has changed');
    });
    

    is likely not in your document ready handler, and is therefore being run before your dom element e1 is ready. You can create a document ready handler, which will run when your dom elements are ready, like this

    $(function() {
        $('#e1').change(function() {
            console.log('e1 has changed');
        });    
    });