drop down menu - jquery: remove spaces from selected state in option dropdown on change event -
in data query, tree structure:
<select name="parent" id="select_parent"> <option> >administrator </option> <option> >manager </option> <option> >sales </option> <option> >operator </option> <option> >promoter </option> </select> obviously looks on combobox options, when select value ... shown complete spaces. want remove spaces when selected value.
i tried this:
$('#select_parent').bind("change",function() { var s = $('#select_parent option:selected').html(); $('#select_parent option:selected').html(s.replace(/ /g, '')); }); but after eliminating spaces ... return see if item no longer has selected attribute?
if you'd display selected list item still spacing while removing spacing in select's displayed text, can try hack:
$('#select_parent').bind("change", function() { var space_offset = 8; var matches = $('#select_parent option:selected').text().match(/\s+?/g); var n_spaces = (matches) ? matches.length : 0; $(this).css('text-indent', -(n_spaces*space_offset)); }); the select's text property immutable, therefore made small css hack un-indent selected option's text inside select's display based on how many spaces has.
don't forget remove spacing besides insde options' texts , adjust space_offset match width of space character in pixels, if page uses different font or font size.
edit: tested on chrome only, firefox it'd require css hacking.
edit: alright, here's final version without css hack:
var trig = ($.browser.mozilla) ? 'click' : 'mousedown'; $(function() { $('#select_parent').on("change", function() { var sel = $('#select_parent option:selected'); $(this).data('cur_val', sel); var display = sel.clone(); display.text($.trim(display.text())); sel.replacewith(display); $(this).prop('selectedindex', display.index()); }).on(trig, function() { if ($(this).prop('selectedindex') == 0) return; var sel = $('#select_parent option:selected'); sel.replacewith($('#select_parent').data('cur_val')); $(this).prop('selectedindex', 0); }); $('#select_parent').change(); }); tested on firefox, chrome, ie , opera. should work on safari well, webkit-based.
Comments
Post a Comment