Home  >  Q&A  >  body text

Need a jquery script to move selected options from a select box based on the end user's current selection

I have an HTML page where the end user can rank our items via a select box element.

See the fiddle to see a simple demonstration:

https://jsfiddle.net/Balkanlii/ghecv1j8/1/

I want to build a jquery script that changes the selected option if the same ranking is selected a second time (if the end user changes their mind, etc.). so:

I have built a query that can be used successfully for this purpose, but the script is broken due to the following conditions:

How to fix it?

Also, is there a better way than mine, I'd like to know since how I do it is getting more complicated.

Any help would be greatly appreciated.

My jQuery script so far:

var previousValue = 0;
$("select[class='myclass']").on('focusin', function(){
     previousValue = $(this).val();
});

$("select[class='myclass']").on('change', function (event) { 
    var prevValue = parseInt(previousValue);
    var selectedValue = parseInt($(this).val());
    var selectedId = $(this).attr('id');

    $('#' + selectedId + " option").removeAttr("selected");
    $('#' + selectedId + " option[value=\""+selectedValue+"\"]").attr('selected', true);

    $("select[class='myclass']").each(function (index, element) { 
           var eval = parseInt($('#' + element.id).val());
            if (prevValue !== 0 && selectedValue !== 0) {
                if (eval >= selectedValue && (eval < prevValue) && element.id !== selectedId) {
                       var b = eval + 1;
                        if (b <= 3)
                            $('#' + element.id).prop('selectedIndex', b);
                        $('#' + element.id + " option").removeAttr("selected");
                        $('#' + element.id + " option[value=\"" + b + "\"]").attr('selected', true);

                }
                else if (eval <= selectedValue && (eval > prevValue) && element.id !== selectedId) {
                       var b = eval - 1;
                       $('#' + element.id).prop('selectedIndex', b);
                       $('#' + element.id + " option").removeAttr("selected");
                       $('#' + element.id + " option[value=\"" + b + "\"]").attr('selected', true);
                            }
                }
                else if (prevValue == 0) {
                            if (selectedValue > 0) {
                                if (eval >= selectedValue && element.id !== selectedId) {
                                    var b = eval + 1;
                                    if (b <= 3) {
                                        $('#' + element.id).prop('selectedIndex', b);

                                        $('#' + element.id + " option").removeAttr("selected");
                                        $('#' + element.id + " option[value=\"" + b + "\"]").attr('selected', true);
                                    }
                                }
                            }
                }
        });
 });

P粉170858678P粉170858678156 days ago3493

reply all(1)I'll reply

  • P粉301523298

    P粉3015232982024-04-05 16:02:41

    You can disable selected options in other fields by performing the following operations. Here's a screen recording of it working...

    (function($) {
    
      const $all = $('select.myclass');
      
      $all.on('change input', function() {
    
        $all.find('option[disabled]').prop('disabled', false);
        
        $all.each(function() {
          const $this = $(this);
          const value = $this.val();
          
          $all.not($this).find(`option[value="${value}"]`)
            .prop('selected', false)
            .prop('disabled', !!parseInt(value));
        });
      });
    
    })(jQuery);
    sssccc
    Project 1
    
    Project 2
    Project 3

    reply
    0
  • Cancelreply