I'm having trouble understanding why a single click in my code is selecting text

xor

I'm trying to make a little menu, that opens when you click on it, and closes when you click outside it. I managed to do that, mostly. The button I press to open the menu has some padding, and when I click specifically on the padding the menu that opens has all it's text selected, which is weird, and I don't understand why it happens. I have looked through MDN's documentation, my favorite search engine, and IRC(freenode) for a solution, but so far no luck.

I made a minimal working example, which I link to at the bottom, and I added a few comments to it about some lines you can comment to change the behavior. Simply, you can press the blue square, and the letter 'a' will show up selected, the expected behavior is that the letter 'a' should show up, but unselected. If you understand what is going on please let me know. ^_^

Edit: I see some discussion bellow about different results on different browsers. I'm currently using Firefox 59.0.1 64-bit on Linux(Fedora 27). A suggestion was made that this might be a bug, I can't rule that out.

https://jsfiddle.net/16k672tt/4/

function outside_function(event) {
    var outside = event.target;
    outside.classList.remove("outside");
    var menu_list = document.getElementById("menu-list");
    menu_list.classList.remove("menu-list-open");
    event.stopPropagation();
  }
  var outside = document.getElementById("outside");
  outside.addEventListener("click", outside_function);

  function menu_click_event(event) {
    var menu_list = document.getElementById("menu-list");
    var outside = document.getElementById("outside");
    menu_list.classList.add("menu-list-open");
    outside.classList.add("outside");
    event.stopPropagation();
  }
  var menu = document.getElementById("menu");
  menu.addEventListener("click", menu_click_event);
.menu {
  /* If you comment the next line, it renders the way I expect */
  display: flex;
  width: 2.5rem;
  height: 2.5rem;
  background-color: #0000ff80;
}

.outside {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.menu-list {
  display: none;
}

.menu-list-open {
  display: block;
  /* If you comment the next line, it renders the way I expect */
  /* In fact if you decrease the alpha value to 0 it works as well (#ffffff00) */
  background-color: #ffffffc0;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <div id="outside"></div>
    <div id="menu" class="menu">
      <div id="menu-list" class="menu-list">
        a
      </div>
    </div>
  </body>
</html>

xor

I had a little talk with emilio on the #servo channel of mozilla's IRC network. He suggested the mouse down event is being triggered on the menu element, and the mouse up event is being triggered on the outside element. The browser assumes this was a drag motion, and selects the text in between. The problem seems to be the mouse up event is placed in the event queue after the click event. So even if the event propagation is stopped, there is already another event on the queue, mouse up, that will end up targeting the newly placed outside element. This could be a bug, so I'm off to https://bugzilla.mozilla.org/ to see if that is the case.

There have been work-arounds suggested, like using CSS's user-select to make the text unselectable, and the use of Javascript's preventDefault() to avoid running the default event handlers. Other stackoverflow users mentioned these but were unfortunately down-voted, and subsequently deleted their answers. I'm leaving these work-arounds here anyway, for anyone that might need them.

Thanks to all the people that helped look into this issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I'm having trouble understanding how object references work in Java?

I'm having trouble with my geometry program

I'm having trouble with my Accordion using JavaScript

I'm having trouble with my websites background

I'm having trouble having my code execute the function chosen by user input

I'm having trouble converting text into a tag with regex

i'm having trouble with my django code while i deleting the data from the table form on click of the delete button

I'm having trouble understanding the syntax used in a piece of code

I'm having trouble understanding a particular piece of syntax

I'm having trouble troubleshooting my function

I'm having trouble with my stack pop() method

I am having trouble understanding part of an ExceptionError code

Having trouble understanding a portion of code

I'm having trouble dynamically allocating my struct

Trouble understanding why I get Segmentation fault (core dumped) in my code

I'm having trouble with my code String and int

I'm having trouble with my handleCheckBox function

(About Radix Sort) I'm in trouble understanding the code from my professor

With my code, I'm having trouble with adding a loop to the end for the code to start over

In "inherit", I'm having trouble with the 'return' code

I'm having trouble understanding a syntax error in Python 3.7

I am having trouble understanding why I am not able to .map over the data I am return in the state that I set up in my React program

Having trouble understanding part of my code, The Fisher–Yates shuffle

I'm new to react and am having trouble with react routing. The pages arent getting displayed. I will attach my code below

Why is there no server.emit in this code and i am having trouble understanding this code in node js?

I’m having trouble figuring out the logic in my custom hook

I'm having trouble with the script code I'm using while listing in my Laravel 8 project

Having trouble understanding this assembly code

I'm having trouble implenting "useContext" with typeScript in my app