Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 297
eriq Avatar asked Dec 28 '11 15:12

eriq


3 Answers

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');
    });    
});
like image 103
Adam Rackis Avatar answered Oct 13 '22 01:10

Adam Rackis


Works for me. Here is the jsFiddle code -

http://jsfiddle.net/CC5P6/

$(document).ready(function() {
  $('#e1').change(function() {
    alert('e1 has changed');
  });
});
like image 21
Kris Krause Avatar answered Oct 13 '22 00:10

Kris Krause


I you haven't done so already you should wrap your code in the $(document).ready(:

 $(document).ready(function() {
    $('#e1').change(function() {
       console.log('e1 has changed');
    });
 });
like image 41
Cyclonecode Avatar answered Oct 12 '22 23:10

Cyclonecode