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

287
Visualizações
Detect click on text in contenteditable div

I would like to allow editing of text when actual text is clicked. The rest of the white space I need for a dragging functionality.

Is there any way to detect when mouse clicks on text instead of white space in a contenteditable div?

Is there maybe some CSS I can add to a div that would wrap around the text and not the whitespace? Or is there some Javascript that can tell when user clicked on text.

enter image description here

      <div class="container">
        <div class="textbox" contenteditable="true"></div>
      </div>
            .container{
              min-width: 100px;
              width: min-content;
              height: min-content;
              padding: 10px;
              border: 1px solid black;
            }
            
            .textbox{
              min-width: 100px;
              width: min-content;
            }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

One solution would be to insert each text in a <p> tag. The <p> will extend on the whole with of the container. Inside <p>, i would wrap each part of text in a <span> tag. So whenever the users clicks on the text, the target of the event would be the span element, and when the users clicks on the whitespace, the target of the event would be a p element.

document.querySelector(".container").addEventListener("click", function(e){
  if(e.target.localName == "p") {
    //whitespace
  }
  if(e.target.localName == "span") {
    //text
 }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

I don't think you can check if someone clicked on the empty space of an element.

The best way to do this is to create div as the border of the textbox and check with javascript if the user clicked on the border.

document.getElementById("drag-border").addEventListener("click",function(event){
  //This if blocks calling rest of function when user clicks on textbox and not on border
  if (event.currentTarget !== event.target) {
    return;
  }
  console.log( "Clicked on border" );
});
.container{
  min-width: 100px;
  width: min-content;
  height: min-content;
}
    
.textbox{
  min-width: 100px;
  width: min-content;
  border: 1px solid black;
  background: white;
}

#drag-border{
  background: red;
  padding: 10px;
  border: 1px solid black;
}
<div class="container">
  <div id="drag-border">
    <div class="textbox" contenteditable="true"></div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
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