0

I have some hidden selects and i want to show the options when clickin in a previous div with a fakeinput class

this is how I'm trying:

$('body').on('click','.fakeinput',function(){
    console.log(true);
    $(this).next('select').show().click();

});
$('body').on('blur','select',function(){
    $(this).hide();
});

But this would only hide/show the select item, it wont show the select's options

what am i missing here?

-EDIT-

Fiddle: http://jsfiddle.net/QAAYd/2/

Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

3 Answers3

1

I'm not sure this is the best solution, but when I had a similar problem I gave the select a size attribute which made it look always open, and gave it a position: absolute so that it would "float" above all else.

$(this).next('select').attr('size',
    $(this).next('select').find('option').length)
       .show();

DEMO

casraf
  • 21,085
  • 9
  • 56
  • 91
0

While it's a little more involved, you could translate the options into a div that's displayed (instead of the select) and then handle the click event on the displayed div elements:

$('body').on('click', 'form > div', function (e) {
    var self = $(this),
        options = self.find('select option'),
        div = $('<div />'),
        select = div.clone().addClass('pop-up-selector');
    $('.pop-up-selector').remove();
    options.each(function (i, elem) {
        var opt = $(elem);
        select.append(div.clone().data('value', opt.val()).text(opt.text()));
    });
    console.log(true);
    select.appendTo(self);
});
$('body').on('click', '.pop-up-selector > div', function (e) {
    var self = $(this),
        val = self.data('value'),
        text = self.text(),
        overlabel = self.parent().siblings('.overlabel');
    // display to user and store value for later
    overlabel.find('.fakeinput').data('value', val).text(text);
    // select corresponding option in select element in case you're using that later
    overlabel.find('select option').each(function (i, elem) {
        var opt = $(elem),
            selected = opt.val() === val;
        opt.attr('selected', selected);
    });
    window.setTimeout(function () {
        $('.pop-up-selector').remove();
    }, 0);
});

and some basic CSS so it looks a little better:

.pop-up-selector {
    background-color: #FFF;
}
.pop-up-selector > div:hover {
    background-color: #CCC;
}

Working demo: http://jsfiddle.net/QAAYd/5/

pete
  • 24,141
  • 4
  • 37
  • 51
0

My solution was to make the select with opacity:0 and to put it on top of a fake button...

select{ position:absolute; top:0; left:0; height;100%; width:100%;opacity:0; }
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378