Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
93
rated 0 times [  100] [ 7]  / answers: 1 / hits: 30066  / 12 Years ago, sat, september 1, 2012, 12:00:00

This question has already been asked but I am stuck with the most basic level of it. I haven't added anything to my html except a select tag and trying to catch the change event by jquery. Here is my code:



<head>
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js></script>
<script>
$('#target').bind(change, function{
alert('Changed');
});
</script>
</head>
<body>
<form>
<select id=target>
<option value=option1 selected=selected>Option 1</option>
<option value=option2>Option 2</option>
</select>
</form>
</body>


Even the change() function does not work.



<script>
$('#target').change(function() {
alert('Changed');
});
</script>


Please help me figure out where I am doing wrong. In one of the similar problems on Stackoverflow, I got http://jsfiddle.net/78x9Q/4/ as an answer. But even an exact copy paste of that code is not working for me.



P.S.: jQuery is loading as I tested it and found working using this:



<script>
$(function() {
alert('Changed');
});
</script>

More From » jquery

 Answers
15

Your code does not work because you try to work with dom when it is not built yet. Use this:



$(document).ready(function () {
// your code
});


Then your code will run when dom is already built.


[#83305] Thursday, August 30, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mattieparisp

Total Points: 612
Total Questions: 109
Total Answers: 104

Location: Trinidad and Tobago
Member since Fri, May 8, 2020
4 Years ago
;