Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6989693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:14:50+00:00 2026-05-27T19:14:50+00:00

How to set index of a multiple select object to be selected when mouse

  • 0

How to set index of a multiple select object to be selected when mouse moves over that index’s option. for example in the next html code, when mouse move over option 1, it will be selected. I want to do it all in JavaScript without editing the html code.

<select size="6" multiple="multiple">
        <option value="1">option 1</option>
        <option value="2">option 2</option>
        <option value="3">option 3</option>
        <option value="4">option 4</option>
        <option value="5">option 5</option>
        <option value="6">option 6</option>
</select>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:14:51+00:00Added an answer on May 27, 2026 at 7:14 pm

    What happens is that explorer does not fire any events for the options tag, nor does it allow us to get its coordinates or dimensions so i think that the only thing left to do is trick the browser a little:

    1. Get the [select] object using its ID.
    2. Next, get the first [option] object (for some reason not all of the
      children of the select object are options… i guess some are text
      nodes because we use white space to indent the HTML, so i’m using the
      second child (options[1]) in order to obtain a reference to the
      first [option] element.
    3. Create a dummy div element which will be used as a hidden canvas.
    4. Set the div to absolute position & visibility hidden so it won’t we
      displayed and won’t effect the content.
    5. Set the div’s height to the size specified in the [option] tag
      font-size style (this is the trick, i’m trying to calculate the
      height of the [option] element by its font-size. sometimes this
      value is specified in points (e.g. 10pt) so i’m creating a div with
      exactly the same value for its height and asking the browser to give
      the height back to me in pixels. Once i have the height of the
      [option] element, the rest is trivial.
    6. subtract the top most position of the select from the y coordinate
      of the mouse and divide by the height of the [option] element. This
      will give us the element on which the mouse is currently positioned
      (mouse top position – select top position converts from screen
      coordinates to the select box coordinates and dividing by the height
      of the [option] element gives us the current [option].
    7. Take the number of the current [option] and use it as the value for
      selectedIndex.

    Code:

        function ieElementFromPoint( e )
        {
            var select  = document.getElementById( "mySelect" );
            var options = select.childNodes;
            var d = document.createElement( "DIV" );
            d.style.position = "absolute";
            d.style.visibility = "hidden";
            d.style.height = options[ 1 ].currentStyle.fontSize;
            document.body.appendChild( d );
            select.selectedIndex = ( Math.round( ( ( e.clientY + document.body.scrollTop ) - select.offsetTop ) / d.offsetHeight ) );
         }
    

    For the rest of the browsers the treatment is much simpler:

    var old = null;
    
    function select( e )
    {
        if ( document.all )
        {
            ieElementFromPoint( e );
        }
        else
        {
            var option = e.target;
    
            if ( option.tagName == "OPTION" )
            {
                if ( old != null )
                {
                    old.selected = false;
                }
    
                old = option;
                option.selected = true;
            }
        }
    }
    

    Don’t forget to give the [select] the proper id (id=”mySelect”) & add onmousemove=”select( event )” on the [select] as well.
    This worked for me on: Chrome, FireFox (3.6), Explorer 8, Explorer 6 (emulated), Opera & Safari.

    Remember to remove the test DIV from the document when we’re done with it, otherwise there will be a bunch of unused DIVs in the DOM, so at the end of ieElementFromPoint() add:

    document.body.removeChild( d );

    Hope this help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C#: Is there a way to SET the selected index of an item in
Does anyone know of a simple way to set a selected index or item
I wanted to set .htaccess to rewrite: example.com/bla/info/ to example.com/info/index.php?q=bla example.com/bla/info/txt/ to example.com/info/index.php?q=bla&t=txt I
How do you get the proper index of a selected input from a set
I'm trying to use mock to verify that an index property has been set.
I have a website that has multiple languages. The way this is set up
I know there are multiple ways to select a particular option from a using
The first index is set to null (empty), but it doesn't print the right
Is z-index only used when you set an element's position to absolute or it
Indexing Service is set up on our file server to index a couple dozen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.