rgbflawed
rgbflawed

Reputation: 2157

Unselect a Select Input

I have a "select" input that is programmed to open up a modal box when clicked to get some information before proceeding. That part all works great.

The problem is that once the modal box is up, the select dropdown options are all still visible. I want that select input to go back to being a normal, not clicked on at all, select box.

What javascript or jquery code can I use to make that select dropdown clear away?

Upvotes: 0

Views: 484

Answers (4)

Shawn Khameneh
Shawn Khameneh

Reputation: 472

Try using this instead:

$('#mySelect').focus(function(event){
    event.preventDefault();
    // code here
});

If that does't work, try using the preventDefault() with the click event. The focus will at least allows users navigating fields with the keyboard (tab, etc) instead of the mouse.

Upvotes: 1

Tomas
Tomas

Reputation: 59575

I'm not sure that you can do it with standard select tag. Maybe because it still has focus. What I did when I needed a customized select tag is to avoid the select tag completely and use a button which graphically looks like the select button. Look at this page - look at the TAX button and the button to the left of it. There is no select tag, but it works great.

Upvotes: 0

Dim_K
Dim_K

Reputation: 571

I think it is more correct to move handler from click to change. In this case select will be close and keyboard changes also will be processed

Upvotes: 1

John Hartsock
John Hartsock

Reputation: 86892

Prior to jQuery 1.6

$('#mySelectBox :selected').attr('selected', '');

jQuery 1.6 and higher

$('#mySelectBox :selected').removeProp('selected', '');

Upvotes: 0

Related Questions