views:

48

answers:

3

Having a problem with the change() event in jQuery:

$("select[name=cmm]").change(function(){
    total();
});

For some reason the "total" function isn't being called with the code above. The html code is:

  <select name="cmm">
      <option value="none">none</option>
      <option value="one">the first</option>
      <option value="two">the second</option>
  </select>

Am i referring to the element the wrong way or something? Or calling the function the wrong way?


Update: The alert function here doesn't show up, so I'm guessing the change event is never called..

$(document).ready(function() {
$("select[name=cms]").change(function(){
    total();
    alert($("select[name=cms]").length);
});
});

(and yes, I have been using doc-ready all the time)

+2  A: 

For updated question:
Make sure your code is running in a document.ready handler so the <select> element is in the DOM and ready, like this:

$(function() {
  $("select[name=cmm]").change(function(){ total(); });
});

For previous question:
You're missing the second m on your selector, it should be:

$("select[name=cmm]").change(function(){ total(); });

Or shorter:

$("select[name=cmm]").change(total);

For the second, just use .val() directly on the <select> to get the value, like this:

var iets = $("select[name=cmm]").val();
//or when calling .change(total) like above, inside total you can use:
var iets = $(this).val();

When you specify option it gets the value of the first <option>, regardless of what's selected.

Nick Craver
Hey, sorry, I don't have that typo in my code, must have gone something wrong while copying it here. Als the short notation method is breaking other JS code for me (i'm using a plugin to style the select menu)
Sled
@Sled - If you're changing your code when asking the question you're likely to get more error, like the one above, make sure your code matches. If the above doesn't work, is it being run inside a `document.ready` handler? Is your `<select>` being added dynamically, via ajax for example?
Nick Craver
Yea sorry about that but there's a lot of code. It was breaking because the paranteses were missing on the total function; $("select[name=cmm]").change(total); it works with this though: $("select[name=cmm]").change(total()); ------ Still it's not being called on time i think because total displays the result in a div, and the result only changes when i "change" on of the other elements that affect the result (two sliders) so the value is stored on "iets" but not changed
Sled
the two sliders i was talking about call the total function too, and they're both working, it's only broken for the "select" element
Sled
@Sled - When you're calling it with `total()` *inside* it's executing *immediately*, not on change, try do `alert($("select[name=cmm]").length);` at the same point, what's the result?
Nick Craver
when I place that line inside the change function for the select element, the alert does not show up. Outside the change function (but stil inside doc-ready it gives me "1"...
Sled
+1  A: 

I'll assume it's not the cm vs. cmm issue, and that you're probably assigning the onChange handler too early, meaning the DOM has not yet been initialized.

<script type='text/javascript'>//<![CDATA[
    // if your javaScript is initialized before the actual
    // select element liek this, it will not work
    $("select[name=cm]").change(function(){
        total();
    });

    function total() {
        alert("works?");
    }
</script>
//]]></script>

<select name="cm">
  <option value="none">none</option>
  <option value="one">the first</option>
  <option value="two">the second</option>
</select>

Your javaScript should be executed after the DOM is ready, so you'll have to use jQuery's document.ready event like this:

<script type='text/javascript'>//<![CDATA[
    $(document).ready(function() {
      $("select[name=cm]").change(function(){
        total();
      });

      function total() {
        alert("works?");
      }
    }
</script>
//]]></script>

<select name="cm">
  <option value="none">none</option>
  <option value="one">the first</option>
  <option value="two">the second</option>
</select>

Or place it after the element in question, like this:

<select name="cm">
  <option value="none">none</option>
  <option value="one">the first</option>
  <option value="two">the second</option>
</select>


<script type='text/javascript'>//<![CDATA[
    $("select[name=cm]").change(function(){
        total();
    });

    function total() {
        alert("works?");
    }
</script>
//]]></script>
luklatlug
hey, I was using $(document).ready() it must be something else. Thanks though!
Sled
A: 

I was having the problem because of a plugin I was using to style the select element. The solution to my problem (being that the onchange function wouldn't work) is here: http://code.google.com/p/lnet/issues/detail?id=41

Sled