Prevent select's dropdown list from being rendered

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • relaxedrob@optushome.com.au

    #1

    Prevent select's dropdown list from being rendered

    Hi All,

    I want to write a select control and use a Javascript function to
    handle all click events on the control. Under certain circumstances I
    also wish to prevent the dropdown's list from being rendered.

    Here is a sample showing what I am _unsuccessfully _ trying to do. :-/

    <html>
    <head>
    <title>Test</title>
    <script type="text/javascript">
    function selectOnclick(e ) {
    // e gives access to the event in all browser
    if (!e) var e = window.event;
    if (e) { e.cancelBubble = true; }
    return false;
    }
    </script>
    </head><body>
    <form name="test">
    <select name="select" onmousedown="se lectOnclick();" >
    <option value="one">one </option>
    </select>
    </form>
    <script type="text/javascript">
    document.test.s elect.onmousedo wn = selectOnclick;
    </script>
    </body>
    </html>

    Any advice would be most appreciated!

    Rob
    :)
  • RobG

    #2
    Re: Prevent select's dropdown list from being rendered

    relaxedrob@optu shome.com.au wrote:[color=blue]
    > Hi All,
    >
    > I want to write a select control and use a Javascript function to
    > handle all click events on the control. Under certain circumstances I
    > also wish to prevent the dropdown's list from being rendered.[/color]

    I don't think you can do this. The only thing I can think of is to
    disable/re-enable the control, but you have to re-enable it from a
    separate function using setTimeout().

    I've added a few comments on your code too.
    [color=blue]
    >
    > Here is a sample showing what I am _unsuccessfully _ trying to do. :-/
    >[/color]

    Please don't use tabs in code, indent by 2 or 4 spaces and replace
    tabs with spaces. Then manually wrap at say 70 characters.
    [color=blue]
    > <html>
    > <head>
    > <title>Test</title>
    > <script type="text/javascript">
    > function selectOnclick(e ) {
    > // e gives access to the event in all browser
    > if (!e) var e = window.event;[/color]

    e = e || window.event;

    will do just as well. But you haven't passed event from the
    onmousedown anyway.
    [color=blue]
    > if (e) { e.cancelBubble = true; }[/color]

    This will only cancel the event in IE, it has no effect in other
    browsers. To add canceling for other browsers, add:

    e.cancelBubble = true;
    if (e.stopPropagat ion) {
    e.stopPropagati on();
    }
    [color=blue]
    > return false;
    > }
    > </script>
    > </head><body>
    > <form name="test">
    > <select name="select" onmousedown="se lectOnclick();" >[/color]

    Need to pass event to the function, disable, etc.:

    <select name="select" onmousedown="
    this.disabled = true;
    selectOnclick(e vent);
    alert('hi');
    setTimeout('ena bleThis()',10);
    ">

    If you comment out the alert, you won't even see the select
    disable/renable, it just refuses to drop down the option.
    [color=blue]
    > <option value="one">one </option>
    > </select>
    > </form>
    > <script type="text/javascript">
    > document.test.s elect.onmousedo wn = selectOnclick;[/color]

    Why add the function a second time? This will overwrite the
    onmousedown on the element so remove it. I put my re-enable function
    here but you could add it elsewhere:

    function enableThis(){
    document.forms['test'].elements['select'].disabled = false;
    }
    [color=blue]
    > </script>[/color]

    Having done the above, you don't need to cancel the event if the
    purpose was only to stop the drop-down.

    I'm not sure doing this at all is a good idea, playing with the user
    interface just annoys users. Is there no other way to do what you
    are trying to achieve?

    --
    Rob

    Comment

    Working...