Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change() on <select> and firefox

I have a dropdown that triggers an ajax call when its changed:

$('.travel-to').change(function(){  
    $.ajax({
        type: "GET",
        url: "/inc/rates/rates-viewer.php",
        data: "shtech=y&c_name="+escape($(this).val()),
        success: function(html){
            $(".rates-viewer").html(html);
            $(".rates-viewer tr.numbers td").css({ opacity: 0 }).fadeTo("slow",1);
        }
    });
});

My problem is, in Firefox, using the up/down cursor keys to cycle through the dropdown options, doesn't trigger the js onChange() event. It's ok in IE.

How can I make Firefox see the up/down cursors as an onChange? Can I do an either/or on the event, to make either an onChange or a keypress trigger the same thing?

Thanks.

like image 490
Chris J Allen Avatar asked Feb 25 '09 17:02

Chris J Allen


2 Answers

You're actually taking advantage of a bug in IE. Firefox supports onChange correctly, as it's not supposed to fire until the browser loses focus of the select field. (I answered a question yesterday about this very issue, in fact.) It's actually kind of dangerous to use onChange with a select, especially because there's no way for keyboard only users to skip some options. (On top of that, the mouse wheel seems to spin by multiple answers, but it actually fires onChange for each entry it passes by on IE.)

If you really want the event to fire whenever someone presses up or down, I would hook into the onKeyPress or onKeyDown events to fire whenever the "up" or "down" key is pressed.

like image 138
Dan Lew Avatar answered Sep 20 '22 13:09

Dan Lew


better option is use .on() to bind function as multiple events

$("#member").on("change keyup", function(){
    -----
});
like image 27
Mo. Avatar answered Sep 18 '22 13:09

Mo.