Answer the question
In order to leave comments, you need to log in
Transferring an option between two selects?
There are two selects, one is all the services of the enterprise, the second is only the selected services. All services are formed in the left select and when clicked they are transferred to the right one. When you click on the right, they are transferred back.
Everything is beautiful, everything works.
I transfer like this:
$('#select1 option').live('click', function() {<br>
var html = this.outerHTML;<br>
$(this).remove();<br>
<br>
$('#select2').append(html);<br>
})<br>
$('#select2 option').live('click', function() {<br>
var html = this.outerHTML;<br>
$(this).remove();<br>
<br>
$('#select1').append(html);<br>
})<br>
Answer the question
In order to leave comments, you need to log in
Options are also sorted by name, and value are numbers. That is, value are not in order. I forgot about it.
And index cannot be written, since only the last index is remembered, and if I throw 5 elements to the left and start inserting them at this index, they will be one after another.
I did it in a loop like this (the data-num = 'number in order in the list' parameter was added to the option):
$('#select1 option').live('click', function() {
var html = this.outerHTML;
$(this).remove();
$('#select2').append(html);
})
$('#select2 option').live('click', function() {
var html = this.outerHTML;
var index = $(this).data('num');
$(this).remove();
inserted = 0; // Задаем индикатор вставки оптиона
$('#select1 option').each(function() {
var ind = $(this).data('num');
if (index < ind) {
$(html).insertBefore(this);
inserted = 1; // Оптион был вставлен на место
return false;
}
})
if (inserted == 0) {$('#select1').append(html);} // Если не был вставлен - значит он
// последний в текущем списке, засовываем его в конец
})
It is necessary to write option both in the right select and in the left.
Only in one select are they style="display:none".
When moving an item in one select, we do jQuery('').hide(), and in the other jQuery('').show().
And it will work much faster, since there will be no need to force the DOM.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question