views:

53

answers:

3

I'm trying to create a webpage to merge vendors in our database and have a page with two select fields like this:

<select name="vendor" id="vendor_select_from">
    <option  value="Apple" id="id0">Apple</option>
    <option  value="Vector Resources, Inc" id="id1">Vector Resources, Inc</option>
    <option  value="Dell, Inc." id="id2">Dell, Inc.</option>
    <option  value="Amazon.com" id="id3">Amazon.com</option>
</select>

Basically, when you select an option in the first field, it should either be disabled or removed from the second field. I could workaround this by simply repopulating the list, but that seems to be massively overkill for what I'm trying to accomplish. That being said, I've yet to figure out a way to do it with javascript or jQuery.

A: 
$(document).ready( function() {
    $("#vendor_select_from").change(
    function(){
        // ...Do something with $(this).val()
        $(this).find("option[value='"+$(this).val()+"']").remove(); // This will remove the selected option from the select widget
    });​​​​​​​​​​​​​​​​
});
Strelok
I'm not sure this is exactly what the OP is after, if the goal is to not allow the same option to be chosen twice...this would remove the options from the second dropdown one by one, and they wouldn't re-appear once the selection on the first changes :)
Nick Craver
Also putting selectors together from strings is dicey. If one of the values had a `'`, \ or other selector-special/invalid character in it, you'd have problems.
bobince
+3  A: 

In your second identical <select>, I changed the ID attributes to end with the number 2.

Then you can easily do something like this:

Try it out: http://jsfiddle.net/3cVqL/2/

$('#vendor_select_from').change(function() {
    var selected = $(':selected', this)[0].id + 2;
    $('#' + selected).attr('disabled','disabled')
        .siblings().removeAttr('disabled');

    var $select2 = $('#vendor_select_from2');
    if(selected == $(':selected', $select2)[0].id) {
        $select2.val('');
    }
}).trigger('change');

HTML

<select name="vendor" id="vendor_select_from">
    <option  value="Apple" id="id0">Apple</option>
    <option  value="Vector Resources, Inc" id="id1">Vector Resources, Inc</option>
    <option  value="Dell, Inc." id="id2">Dell, Inc.</option>
    <option  value="Amazon.com" id="id3">Amazon.com</option>
</select>

<select name="vendor" id="vendor_select_from2">
    <option  value="Apple" id="id02">Apple</option>
    <option  value="Vector Resources, Inc" id="id12">Vector Resources, Inc</option>
    <option  value="Dell, Inc." id="id22">Dell, Inc.</option>
    <option  value="Amazon.com" id="id32">Amazon.com</option>
</select>

Update: Changed it to clear the second <select> if the two match.

Update: Cleaned things up a bit.

patrick dw
Thanks, this did exactly what I was trying to do.
Swimming Code
@Swimming - You're welcome. Remember to click the checkmark next to this answer to confirm that you've "Accepted" it. :o)
patrick dw
A: 
$('select[id=select1]').change(function()
{
    $('select[id=select2] option').attr('disabled', false);
    $('select[id=select2').find("option[value=$('select[id=select1]').val()]").attr('disabled', true);
});

Note: No validation. you might want to add validation like if the element exists or not.

koderoid