Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

407
Visualizações
Chrome on Android - Select Text on Single Tap and Show Copy Button

I'm building a web app with pure JavaScript. I'd like the user to be able to select an entire paragraph when they tap it once in Chrome Android, and have the "Copy" Android context menu pop up so they can easily copy the text they just selected.

I've already got the entire paragraph being selected on one tap, no problem, using the Selection API. But, the "Copy" menu doesn't come up when the text is selected. Why not, and how can I get it to show up in Chrome on Android?

I've made a JSFiddle isolating my problem which you can open on Chrome Android to check out. Here's the code from it.

HTML:

<p>Lorem ipsum</p>
<p>Dolorum est</p>

JavaScript:

let selectedNode = undefined;

document.querySelectorAll("p").forEach(function (element) {
  element.addEventListener("click", function () {
    const selection = window.getSelection();
    selection.removeAllRanges();

    if (element === selectedNode) {
      selectedNode = undefined;
    } else {
      const range = document.createRange();
      range.selectNodeContents(element);
      selection.addRange(range); 

      selectedNode = element;
    }
  })
});

When I tap either of the paragraphs, the paragraph text is selected, but no Copy dialog comes up:

Text is selected, no copy button

When I tap the paragraph again to DESELECT it (which essentially calls window.getSelection().removeAllRanges(), see the JavaScript above), THEN the Copy dialog comes up:

Text is deselected, copy button appears

I'm curious why this happens, but ultimately I'd like to know:

How can I get the Copy dialog to show up when the text is selected, rather than when it's deselected?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda