Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the select option order?

Tags:

jquery

<select onchange=""class="product-custom-option" name="options[41]">
  <option value=""> -- selsect --</option>
  <option price="0" value="162">L</option>  
  <option price="0" value="163">M  </option> 
  <option price="0" value="164">S </option>  
 <option price="0" value="165">XL </option>

  </select>

now i want to chage the order as this: S M L XL .

$('.product-custom-option:option(3)').insertAfter('.product-custom-option:option(0)');
$('.product-custom-option:option(1)').insertBefore('.product-custom-option:option(4)');

i am sorry, i am a newbie of jquery, the above doesn't get that?

i want to get the new order like this:

  <option value=""> -- selsect --</option>
<option price="0" value="164">S </option> 

  <option price="0" value="163">M  </option> 
     <option price="0" value="162">L</option> 
 <option price="0" value="165">XL </option>
like image 659
hehe Avatar asked Dec 03 '25 11:12

hehe


2 Answers

var options = $('.product-custom-option option' );

$( options[ 3 ] ).insertAfter( $( options[ 0 ] ) );
$( options[ 2 ] ).insertAfter( $( options[ 3 ] ) );

http://jsfiddle.net/ceduG/

But it's probably easier to sort it server-side if at all possible.

like image 138
JJJ Avatar answered Dec 06 '25 03:12

JJJ


Or you can just replace the html of select element :

$('.product-custom-option').html('<option value=""> -- selsect --</option><option price="0" value="164">S </option><option price="0" value="163">M  </option><option price="0" value="162">L</option><option price="0" value="165">XL </option>');​

http://jsfiddle.net/746G9/

like image 34
atif Avatar answered Dec 06 '25 02:12

atif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!