Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire event each time a DropDownList item is selected with jQuery

I have a dropdown: <asp:DropDownList id="dropdownid" runat="server" class=blah"/>

in my jQuery, I assign change event like this:

$('#dropdownid').change(function() {......});

Now, this works when I select different value from the dropdown, however let's say I want to select the same value again. (because I want to make another call with the same value) So, when I select it again, (without changing the value) just by clicking on the selected value from the dropdown and "selecting" it again, no event fires. Is there another event in jquery that I have to assign to it? what's the workaround?

like image 924
ra170 Avatar asked May 22 '09 15:05

ra170


3 Answers

To expand Vincent Ramdhanie's suggestion, take a look at doing something like this. Essentially, you end up with your own jQuery function that you can re-use elsewhere.

Step 1: Create the jQuery Function

(function($) {
    $.fn.selected = function(fn) {
        return this.each(function() {
            var clicknum = 0;
            $(this).click(function() {
                clicknum++;
                if (clicknum == 2) {
                    clicknum = 0;
                    fn(this);
                }
            });
        });
    }
})(jQuery);

Step 2: Make sure that the newly created jQuery Function's file is referenced for use:

<script src="Scripts/jqDropDown.js" type="text/javascript"></script>

Step 3: Utilize new function:

$('#MyDropDown').selected(function() {
    //Do Whatever...
});

ORIGINAL INFO
With your current code base, selecting the same value from the asp:DropDownList will not fire the change event.

You could try adding another jQuery function for the .blur event. This would fire when the control loses focus:

$('#dropdownid').blur(function() {......});

If they blur function doesn't work for you, I'd add a refresh button or something to that affect that fires the function you are trying to utilize.

like image 126
RSolberg Avatar answered Nov 10 '22 03:11

RSolberg


Try this:

$(document).ready(function(){
 var clicknum = 0;
 $("#dropdownid").click(function(){
    clicknum++;
    if(clicknum == 2){
        alert($(this).val());
        clicknum = 0;
    }
 });
});

First you are creating a variable clicknum to track the number of clicks because you do not want the event to fire every time the user clicks the drop down box. The second click is the selection that the user makes.

If click num happens to be 2 then this is a second click so fire the event and reset clicknum to 0 for the next time. Otherwise do nothing.

like image 44
Vincent Ramdhanie Avatar answered Nov 10 '22 03:11

Vincent Ramdhanie


Simply wireup click event handlers to the enclosed option controls instead of the select:

$('#dropdownid option').click(function() {......});

This will only fire when you select an option from the dropdown, regardless of whether it changes.

like image 7
Adam Lassek Avatar answered Nov 10 '22 05:11

Adam Lassek