JQuery small question I need help with

Hey. I need help with a bit of jquery. I rename the dropdowns when I click on the button next to them. I want to get the selected Prev drop-down option value in the code below and assign the checkbox that you click. Hope this 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'); } }); }); 
+4
source share
1 answer

The HTML structure will help a ton, but the first optimization is to cache your first checkbox. Then we use the supposed iteration in jQuery. If I understand what you are trying to do, I get the following:

 $('.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 say exactly from your question whether you want to assign a value to the checkbox in the selection list or vice versa. I went with "set the checkbox name to the value of the select list". Hope you were after.

+1
source

Source: https://habr.com/ru/post/1310436/


All Articles