Events when you close the drop-down list in the select box.

Asked 1 years ago, Updated 1 years ago, 46 views

In order to prevent key-ups or key-downs with the drop-down list closed in the select box, I have considered one of the following actions, but I cannot find the appropriate event (I would like to be able to use the key when I expand the drop-down list).
·Remove focus when selecting elements from the drop-down list
 onI couldn't pick up the selected element with onchange
·Remove focus when the drop-down list is closed

[Code]

<divid="apDivTEST">
        <select name="TEST" id="TEST" style="width:354px;height:40px;font-size:20px;background-color:#000;color:#CFCBC8;border-color:#000;outline:none;">
            <option value="A" id="TEST_1"></option>
            <option value="B" id="TEST_2"></option>
        </select>
   </div>

javascript

2022-09-30 21:24

1 Answers

I want to be able to use the key when expanding the drop-down list

You may want to check the elements where the keyup, keydown event is occurring and allow the event only if it is a select box list.

function isSelectBox(element){
  return element.id === 'TEST'; // Returns true for select box elements
}

document.onkeydown=function(event){
  return isSelectBox(event.target);
};

document.onkeyup=function(event){
  return isSelectBox(event.target);
};

You can disable the event by false by return in the function to register with the event handler.
Therefore, retrieving the origin element of the event (event.target above) and returning true for the select box will only enable the event when selecting the select box.

*Please note that the behavior may vary depending on the target browser.


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.