views:

35

answers:

4

I want prevent user to select dropdown list items using javascript methods. Is there anyway use OnMouseKeyDown so I can stop there. I donot want use Enable=false.

Javascripts gurus, help me out.

A: 

onfocus="this.blur()"

add that to the element

Isisagate
Still user can select. I tried already this.
James123
A: 

let's try this in c#

string hardcode = "1"; // set as appropriate
myDropDownList.Attributes["onchange"] = "this.selectedIndex = " + hardcode + ";";

keep in mind if your user has javascript disabled this isn't going to work, and you should be doing verification on the server no matter what.

lincolnk
That's really bad from a usability perspective. You select item 4, it selects item 1. Not nice! :)
Marko
selected item will be there already. Just user has see the data (not to select).
James123
@Marko of course it is, but this is the least painful way I can think of to accomplish this.
lincolnk
A: 

If you want the user to be able to open the select box but not change the selection, you can do the following:

<select onchange="this[0].selected = true">
<option>1</option>
<option>2</option>
<option>3</option>
</select>

Where you change the selected index to the item you wish to keep selected. As a side note, this usually isn't a very good idea from a user interface perspective.

Herman
A: 

Using jquery

HTML:

<select id="dropdown">
   <option>Select an option</option>
   <option>Option 1</option>
   <option>Option 2</option>
   <option>Option 3</option>
 </select>

JS:

$(function(){
    $("#dropdown").change(function(){
        $(this).find("option:first").attr("selected", "selected");
        return false;
    });
});
T B