Sorting a select with fractions in jquery / javascript -
i sort below select options computed fractional value, excluding --please select-- option.
<select class="wpsc_select_variation"> <option value="0">-- please select --</option> <option value="13">1"</option> <option value="21">2''</option> <option value="11">3/4"</option> <option value="6">3/8"</option> <option value="10">5/8"</option> <option value="17">1-1/2''</option> <option value="15">1-1/4''</option> <option value="19">1-3/4''</option> <option value="7">1/2"</option> </select> code i've set far: added pastebin brevity , history's sake. link
this explain length of note 1 array beingness 3. why 3? why isn't 9? why chrome show total array, still have length of 3, , firefox show total array after clicking parent +, parent says undefined, option, option?
confused...
edits: edit allow multiple select boxes: right me if i'm wrong.
$(".wpsc_select_variation").each(function(index, element){ $($(element).children().get().sort(function (a, b) { homecoming parseint(a.value) - parseint(b.value); })).appendto($(element)); }); latest edit using objects. here goes:
var els = new array(); var counter = 0; $j("select.wpsc_select_variation option").each(function(index, ele){ var obj = new object; var = $j(ele).text(); = i.replace("\"", ""); = i.replace("''", ""); if (i.indexof("-") >= 0){ = i.split('-'); split = i[1].split('/'); = eval(i[0]) + split[0] / split[1]; } else if(i.indexof("/") >= 0){ split = i.split('/'); = split[0] / split[1]; } = eval(i); obj.ind = i; obj.ele = ele; els[counter] = obj; counter++; }); els.sort(function (a, b) { homecoming parseint(a.ind) - parseint(b.ind) }); console.log(els); the above results in:
[object { ind=nan, ele=option}, object { ind=0.75, ele=option}, object { ind=0.375, ele=option}, object { ind=0.625, ele=option}, object { ind=0.5, ele=option}, object { ind=1, ele=option}, object { ind=1.5, ele=option}, object { ind=1.25, ele=option}, object { ind=1.75, ele=option}, object { ind=2, ele=option}] it appears sorting them somewhat, can see, still out of order.
since alternative values in order need, can sort on those:
$($(".select_variation option").get().sort(function (a, b) { homecoming parseint(a.value) - parseint(b.value); })).appendto(".select_variation"); javascript jquery
No comments:
Post a Comment