views:

69

answers:

1

Hi, I'm trying to figure out the best way to handle a dropdown list where one of the options "other" shows a hidden text field (via jQuery), where the user can then enter text. Should both the dropdown and the input field be given the same "name" attribute, then server side code runs a check on the values of each in order to know what one is the active value - e.g if the value of the select box is "other", then check the value of the text input field - if this isn't the default value ("enter text"), then it's ok to use this value and save it to the database. Is this a messy approach to this problem?

EDIT: this is my client side code for show/hiding the "other" field.

$('.select_change').live("click change", function(){
            //check if value is other
            $(this).parent().find(".hidden").toggle($(this).val() == 'other');
        });
+2  A: 

I think the way to go is to check if the value selected is equal to "Other" and then show/hide the input based on the value.

On the server side I would perform a similar check, and store the value from the textbox if the dropdown value is equal to "Other".

I haven't tested this code but I think this is the general idea:

$('#myDrpdown').change(function() {
    if($(this).val() === "other"){
        $("#hiddenInput").show();
    } else {
        $("#hiddenInput").hide();
    }
});
DannyLane
+1 This is the approach I tend to go for. I will say though, for a bilingual website I found the best way to be to test for the *last* element (going by the assumption, and implementation of the 'Other' field always being last in generated selects).
karim79
Hi Danny, this is more or less what I have at the moment (added my code to my post above). It's more an issue of how best to process the submitted data when you have two possible inputs for one value in the database
ted776
@ted I guess it depends on how you get the data to the server, if you were doing something like sending up data in json format you could evaluate which one to use on the client side so that when it gets to the server the value is the correct one to use, the only other option I can see is to check the value of the dropdown on the server side, see if it is set to other, if it is then use the textbox value. You seem reluctant to take this approach, is there a reason?
DannyLane
@DannyLane I am happy enough to use that approach, I was interested to find out if there way a better way to handle this type of situation, as it must be fairly common I think. I think a combination of client and server side checking to pick out the non-default value is most likely the way forward. Thanks again for your help
ted776
@ted, it is indeed a common problem. I have encountered it recently where I work and I solved it the way I described above. best of luck with it.
DannyLane