Hi i want create function which will after clicking button remove option from one list and insert in to another but before
option with class
with grater suffix than one that i selected. Class name is opt_$i
so when i select opt_4
i want to insert it before opt
which is greater (its not always 5 it can be 73)
function minus(id)
{
var c = $('select#'+ id +' option:selected');
var cities = $('select[name=cities] option');
c.each(function(){
var item = $(this);
var current = parseInt($(this).attr('class').replace(/[^0-9]/g, ''));
cities.each(function(){
var next = parseInt($(this).attr('class').replace(/[^0-9]/g, ''));
if(next > current)
{
var next_item = $(this);
return; // I dont know why this doesnt work ??
}
});
/**
if(previouse == 1)
{
$('select[name=cities] option:first').before(
$('<option></option>')
.attr('value', current.val())
.attr('class', current.attr('class'))
.text(current.text())
);
}
else
{
*/
//$('select[name=cities] option[class=opt_'+ previouse +']').next()
next_item.before(
$('<option></option>')
.attr('value', current.val())
.attr('class', current.attr('class'))
.text(current.text())
);
//}
$(this).remove();
});
}
Is there better way to achive this ?? Than looping every time list in which i want to insert option to find greater class value ??
Edit:
<select id="a">
<option class="opt_3">a</option>
</select>
<select id="b">
<option class="opt_1">c</option>
<option class="opt_7">d</option>
</select>
after clicking on button i want to remove option from list a
and insert to list b
but before opt_7
. How to find option which has class greater than opt_3 ??
SOULUTION http://jsfiddle.net/Atm8V/6/ here is how it should work, thanks for all comments.