Skip to content

Instantly share code, notes, and snippets.

@lcruz
Created May 4, 2012 15:07
Show Gist options
  • Save lcruz/2595354 to your computer and use it in GitHub Desktop.
Save lcruz/2595354 to your computer and use it in GitHub Desktop.
Rebuild a select wrapped with jqTransform
function fill_select(id, values) {
$select = $(id);
$select[0].options.length = 0;
$ul = $select.parent().find('ul');
$ul.html('');
$.each(values, function(i, v) {
var index = $select[0].options.length;
$select[0].options[index] = new Option(v.text, v.value);
var oLi = $('<li><a href="#" index="'+ index +'">'+ v.text +'</a></li>');
$ul.append(oLi);
});
// Volver a contectar el onclick
bind_select(id);
$wrapper.find("ul a:first").addClass("selected").html()
}
function bind_select(id) {
$select = $(id);
$wrapper = $select.parent();
$wrapper.find("ul a").unbind('click').click(function (e) {
$wrapper.find('a.selected').removeClass('selected');
$(this).addClass('selected');
$select[0].selectedIndex = $(this).attr('index');
$('span:eq(0)', $wrapper).html($(this).html());
$ul.hide();
return false;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment