views:

493

answers:

4

Folks,

I'm trying to multiple the values of 2 select dropdowns whenever either of them are changed and then display that value.

This is what I have (which of course doesn't work) - Any ideas?

TIA!

EDIT EDIT EDIT EDIT EDIT EDIT

Thanks everyone for the help - I've updated the JQuery to reflect the attempts I've made based on all your posts. No luck yet, but I'm learning a lot :)

EDITED ONE LAST TIME WITH WORKING SOLUTION - Thanks all

$(document).ready(function() {  
 $(".DropChange").change(function(){  
  var ValOne = $('#ValOne').val();
  var ValTwo = $('#ValTwo').val();
  var totalTotal = ((ValOne * 1) * (ValTwo * 1));    
  $('#Total').text(totalTotal);
 }); 
}); 
<select name="ValOne" id="ValOne" class="DropChange">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
   <option value="6">6</option>
</select>

<select name="ValTwo" id="ValTwo" class="DropChange">
   <option value="1">1</option>
   <option value="2">2</option>
   <option value="3">3</option>
   <option value="4">4</option>
   <option value="5">5</option>
   <option value="6">6</option>
</select>
<span id="Total"></span>
A: 

You won't be able to attach a change event handler via jQuery's live function.

jQuery's live function is implemented by taking advantage of event bubbling (a handler is attached to the document, when an event bubbles up, jQuery determines if it should call your handler based on which element caused the event). The select element's change event does not bubble up.

The click event does get bubbled up, but it's not raised when the value changes.

You'll need to approach this differently. One option is to bind directly to the elements' change events (as opposed to using live).

$(".DropChange").change(function(){

instead of

$(".DropChange").live("change", function(){

Another option is to calculate and display the product on a timer.

Ken Browning
Thanks Ken, I tried using "$(".DropChange").change(function(){" with and without the "setTimeout" example blahblah provided and I fixed the total like Josh pointed out. Now I get no errors, but nothing happens :(I put in "alert("dropchange")" and I never see the alert in either case.Any other ideas what I'm missing?
Chris
-1, the 'change' live event handler is currently not supported in jQuery. See http://docs.jquery.com/Events/live
Sbm007
@Sbm007: how is that inconsistent with anything I've said?
Ken Browning
@Chris: Update your post with the code or provide a link to a sample page.
Ken Browning
Thanks Ken, I've updated the main posts with all the tries I've done so far.
Chris
@Ken, my apologies. I'm an idiot for not reading your post completely. Could you edit your post so I can cancel the -1. Again, sorry about that.
Sbm007
+2  A: 

Don't use live, use change event instead.

Also, you are setting the result into an element #display, but I think you mean #Total.

EDIT Since you still haven't figured it out, here ya go...

$(function() {
  $(".DropChange").change(function(){
    var valone = $('#ValOne').val();
    var valtwo = $('#ValTwo').val();
    var total = ((valone * 1) * (valtwo * 1));
    $('#Total').text(total);
  });
});
Josh Stodola
@Chris: This is the same as your 3rd attempt. That works for me in IE and Chrome.
Ken Browning
Thanks Josh and Ken, you guys are right - that does work. I'm not sure why yet but the page also have the jquery.validate.pack.js plugin being loaded. When that is loaded, the above code doesn't work. If I remove it, it works just fine.Thanks Again!
Chris
A: 
<select name="ValOne" id="ValOne" class="DropChange">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
</select>

<select name="ValTwo" id="ValTwo" class="DropChange">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
</select>
<span id="Total"></span>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; 
<script type="text/javascript">
    $(document).ready
    (
     function()
     {
      $(".DropChange").change
      (
       function()
       {
        $("#Total").text($("#ValOne").val() * $("#ValTwo").val());
       }
      );
     }
    );     
</script>
Lobstrosity
A: 
Sorpigal