views:

73

answers:

4

I have the following HTML containing a drop down list (single selection)

        <script type="text/javascript">
            $(document).ready(function () {
                $("#garden").bind('change', function() {
                    alert("Changed");
                    });
            });
        </script>
    <body>
        <div id="content">  
           <select id="garden">
             <option value="flowers">Flowers</option>
             <option value="shrubs">Shrubs</option>
             <option value="trees">Trees</option>
             <option value="bushes">Bushes</option>                 
           </select>
        </div>
    </body>
</html>

When the HTML page is rendered, Flowers is the already selected element in the drop-down by default (being the first one). If I select any other value (other than "Flowers"), the javascript function is fired and a alert box containing "Changed" is shown.

Q1: But, in case I re-select Flowers again, the onchange event is not triggered. I understand it is because nothing was 'changed' in the drop-down list. Is there a way a function is triggered even when no change to the already selected value in drop-down is done?

Q2: How to extract the value of the element which has just been selected (even if nothing new was selected - that is, user clicked on the drop down, and simply clicked somewhere else).

I have already tried the "onblur" but that requires that the user clicks somewhere else on the document so that the drop-down loses focus. Any help would be really appreciated.

Thanks a lot. [I have edited out the HTML headers and other script inclusions in the code snippet provided for brevity.]

A: 

Question 1: I think the event for that would be "onSelect".

Question 2: I think the "onSelect" event would work for that as well, not 100% sure. Something to try at least and mess with.

Brad F Jacobs
Nope, onselect is also not working :(. In fact, onselect is not even getting fired for either selecting drop-down, re-selecting some option, releasing the mouse, clicking anywhere else in the document. Any ideas what it does? (Sorry, I am a novice in javascript and HTML)
Shrey
+1  A: 

Well I think I don't get you 100%, but some things I can suggest here are:

  • bind a click event handler to the select

    $("#garden").bind('click', function() {
         alert($(this).find('option:selected').text());
    });
    
  • bind a focusout event handler

    $("#garden").bind('focusout', function() {
         alert($(this).find('option:selected').text());
    });
    

and of course bind the change event handler which you already got. The click event handler might be a little bit tricky since it would fire every time you click on the element. But if you don't alert() it every time it should not be a problem at all, you got the current selection and can do with it whatever you want.

jAndy
Wow, Thanks for this. It works almost perfectly for me :D
Shrey
+1  A: 

You can manually trigger the event when you load the page:

$(document).ready(function() {
    $("#garden").bind("change", function() {
        // ...
    }).change();
});

This will pick up the initial value - so I think it makes your second question irrelevant. This won't work in all situations (I'm hoping that your actual handler isn't an alert but actually something useful!), but could come in handy...

Jon
Hmm, that is an interesting suggestion. Will get back after trying it out.
Shrey
+1  A: 

Are you sure that whatever you are doing is the most sensible way to do things? It seems like very strange UI to have different behaviour if you select an item by leaving it as the default compared to selecting it by opening up the select and selecting the currently selected item.

If you are wanting to make them explicitly choose flowers then maybe you want a dummy entry at the top that says "Please choose one" that will then mean they are forced to actually change to flowers if that is what they want. It would probably be simpler than complicating your code with more event handlers and such like.

If you do really need to go down the path you are following then you may want to consider what the behaviour is if somebody just tabs to the control and then past it. ie should that fire your script off as well?

Edit to respond to comment at length:

What you will want to do in this case is hook into the onSubmit handler of the form. This is called, as you can imagine, when the form submits. If your handler returns false that form will not be submitted.

This handler is traditionally where you would do client side validation by examining the state of whatever form elements you care about and checking their values are valid. In this case you'd check if the value of garden was "N/A" or whatever you set it to and if so pop up an alert (in the simplest case) and possibly mark whichever fields need attention. Then the user will choose a valid entry (hopefully) and next time he submits your validation will succeed, you return true in the handler and the user can be happy he submitted valid input.

As always though the standard disclaimer that any data can be sent to your server by a determined user so you should not assume that just becasue you had this validation that you are getting valid data on the server. :)

Chris
@Chris, Actually, what I asked was just because I was trying out event handling before incorporating in my application. I do understand that present situation (default vs explicit selection based js trigger) is a bad UI - and "Please choose one" is exactly what I want. Problem is that user may end up choosing "Please choose one" in which case I need to convey to him that your actions are not valid (I can be passive as well - doing nothing - but that is not what my aim is). Another issue being, restricting him not to select "Please choose one" - any hints on how to do this?
Shrey
@Shrey: I responded to your question in my answer so it didn't get scrunched into a comment (which I think it was too logn for anyway).
Chris
@Chris : Thanks a lot for your answer. That does make sense and seems the most obvious thing I should have done :) .. Thanks once again. (Bumping up the comment as I can't do the same for 'answer' twice).
Shrey
@Shrey: My pleasure. Its what this place is here for. :)
Chris