views:

439

answers:

6

is there any way i can trap html select events and can prevent the html select dropdown to open (disabling html select is ruled out)

A: 

This doesn't disable the "dropdown to open", but if you don't want anything selectable, a trick I used was to make <optgroup> instead of option. However, I'm confused why you would want to disable the dropdown, but disabling it is not option...

Myles
Hi Myles, i want the events for the dropdown to be raised so that i can handle them on my own but i dont want that html select should open its own dropdown. Disabling the html select will prevent the events from html select element to be raised.
deepak
So you're making a custom drop down then? In that case, I wouldn't use a drop down at all. I would use an <ul>, subscribing to the proper events and using CSS to simulate a drop down, then to post the value, use a <input type="hidden"> whose value is set via javascript.
Myles
A: 

If you don't want to drop down a drop down box then why make it a drop down control.

Use an image that looks like a drop down and set it as the background if you need to get the feel of a drop down box.

rahul
Thanks, can you please give a example or reference where i can find this approach.
deepak
Why suggest using an image? Select boxes look different on each browser and operating system so if you wanted to use this method on a page with other select boxes that used the default action, they wouldn't match up in each browser. Also, it could have been possible the asker only wanted to temporarily disable the default action, or fall back to the default action if javascript is not enabled in the visitor's browser settings.
Andy E
+1  A: 

I doubt this will actually prevent it from opening, but it will ensure that the DropDown will always maintain the same value:

<select name="theselect" onchange="this.selectedIndex = 1;">
    <option value="Red">Red</option>  
    <option value="Green" selected="selected">Green</option>  
    <option value="Blue">Blue</option> 
</select>
Dominic Barnes
A: 

This works great for me in IE and Chrome, there's no flicker or anything:

html

<select id="MySelect"><option>Hello</option></select>

js

MySelect.onmousedown = function ()
{
    window.setTimeout(function () 
    { 
        //- An immediate blur, then refocus stops the options from being displayed
        this.blur();
        this.focus();
        //- so now we run our custom function
        runOtherFunctionInstead(); 
    },0);
}

Make sure the js runs after the select element has been parse by placing it in an onload or ondocumentready or a script block after the select element. Haven't tried it in Firefox or Opera. Assumedly it would work in Safari, though.

Andy E
A: 

I second what Myles says: You hope you find these links useful:

Making custom dropdowns http://jonathan.tang.name/code/jquery%5Fcombobox

Demo: http://jonathan.tang.name/files/jquery%5Fcombobox/demo.html

Kieran
A: 

You could remove all of the options from the dropdown, or hide the current select element and replace it with an empty one.

<select id="main">
 <option name="1">1</option>
 <option name="2">2</option>
 <option name="3">3</option>
</select>

<select id="empty" style="display:none;">
</select>


<script>
function disableSelect() {
    document.getElementById('main').style.display = 'none';
    document.getElementById('main').style.display = '';
}
</style>
Ian Silber