Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing select menu from opening

Possibly a silly question, but how do I prevent a select element in a form from showing its drop down menu when it's clicked on? I tried the following:

$('select').click (function (e) {     console.log (e);     return false; }); 

and

$('select').click (function (e) {     e.preventDefault ();     console.log (e); }); 

But neither worked.

What am I doing wrong?

EDIT: The reason I need to know is for a jquery enhanced select element that needs to degrade gracefully. The idea is the select, when clicked, opens a jquery UI dialog with a nicely maked up list that the user makes their selection from (clicking a list item causes the select's value to update). If JS is disabled then the select should just operate as normally.

The problem is that as well as the dialog opening, the dropdown also appears, which is not what I want. I can't just disable the control, as its value needs to be submitted along with the rest of the form.

like image 908
GordonM Avatar asked Nov 30 '11 08:11

GordonM


People also ask

How do I turn off select option?

The disabled attribute can be set to keep a user from selecting the option until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript is required to remove the disabled value, and make the option selectable.

How do you turn off selection in HTML?

The disabled attribute for <select> element in HTML is used to specify that the select element is disabled. A disabled drop-down list is un-clickable and unusable. It is a boolean attribute.


2 Answers

This should work:-

$('#select').on('mousedown', function(e) {    e.preventDefault();    this.blur();    window.focus(); }); 
like image 68
Deniz Ozger Avatar answered Oct 02 '22 12:10

Deniz Ozger


The problem is that you're using the wrong event.

<select onmousedown="(function(e){ e.preventDefault(); })(event, this)">   <option>Some Option</option> </select> 

JsFiddle

like image 37
Allenph Avatar answered Oct 02 '22 10:10

Allenph