Problem with change event on a select box
Hi all, I'm currently having a problem with getting a change event to fire when changing the values of a select box. I've put together the sample code below to show what I mean.
- <html>
- <head>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
- <script type="text/javascript">
- $(function() {
- $("select[name='test1']").change(function() {
- alert($(this).attr("value"));
- });
- });
-
- function changeValues() {
- $("select[name='test1']").attr("value","2");
- }
- </script>
- </head>
- <body>
- <div>
- <select name="test1">
- <option>Please Select</option>
- <option value="1">One</option>
- <option value="2">Two</option>
- <option value="3">Three</option>
- </select>
- </div>
- <br />
- <div>
- <button onclick="changeValues();">Change Value</button>
- </div>
- </body>
- </html>
Basically, when you manually change the value of the select box the change event fires but if you press the button to change the value then no event fires.
I have a feeling I'm missing something really obvious but at the moment I just can't see what's wrong. Can anybody help with this?
Thanks