tags:

views:

176

answers:

1

Hi I need help with a bit of jquery, I am renaming dropdown lists when a checkbox next to them is clicked. I want to get the selected option value of the dropdown called 'Prev' in the code below and assign to the checkbox that is clicked. I hope it makes sense. Thanks

$('.mutuallyexclusive').live("click", function() {


            checkedState = $(this).attr('checked');
            $('.mutuallyexclusive:checked').each(function() {
                $(this).attr('checked', false);
                $(this).attr('name', 'chk');
            });
            $(this).attr('checked', checkedState);

            if (checkedState) {
                jQuery('#myForm select[name=cat.parent_id]').attr('name', 'bar')

                // here is the bit i need help with
                // get the selected option of the dropdown prev and set it to $(this).val.. something along those lines
                var prev = $(this).prev('select').attr("name", 'cat.parent_id');

            }
            else {
                var prev = $(this).prev('select').attr("name", 'dd');
            }

        });
    });
+1  A: 

The HTML structure would help a ton, but the first optimization is to cache your initial checkbox. Next, take advantage of implied iteration in jQuery. If I understand what you're attempting to do I end up with this:

$('.mutuallyexclusive').live("click", function() {

    var $check = $(this);

    var checkedState = $check.attr('checked');

    $('.mutuallyexclusive:checked')
      .attr('checked', '')
      .attr('name', 'chk');

    $check.attr('checked', checkedState);

    if (checkedState) {
        $check.attr('name', $check.prev('select').val());
    } else {
        $check.attr('name', 'dd');
    }

});

I couldn't tell for sure from your question whether you wanted to assign the value of the checkbox to the select list, or vice versa. I went with "set the name of the checkbox to the value of the select list". Hopefully that's what you were after.

g.d.d.c