How can I have jQuery code further optimized?
I have few hints save jQuery in $ variable:
(function($) {
   // your code
})(jQuery);

$ is common for jQuery and it's easier to read because it shorter.
Second and obvious is using loop to run calls in:
    if (selectedValues.length &gt; 0) {
       selectedValues.forEach(function(value, i) {
          var num = i + 1;
          $(`input[name=gift_${num}]`).val(value);
       });
    }

and further improvement is to move that into a function. So instead of comment:
// Set hidden input with the chosen gift.

you can have:
setHiddenInputs(selectedValues);


function setHiddenInputs(values) {
    values.forEach(function(value, i) {
        var num = i + 1;
        $(`input[name=gift_${num}]`).val(value);
    });
}

I would also merge those two lines:
var selectedValues = [];
selectedValues = $('#ff_124_multi_select_gifts').val();

you assign array for no reason and then replace it with output of val() call:
var selectedValues = $('#ff_124_multi_select_gifts').val();

also if selectedValues are gifts then you can use:
var selectedGifts = $('#ff_124_multi_select_gifts').val();
setHiddenInputs(selectedGifts);

which is exactly what your comment try to say