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

202
Visualizações
get entire line from a textarea and editable div

right click inside a textarea gives entire line - regarding the current cursor position
I need the same with an editable div - but it doesn't work
console is empty i.e. an empty string is there
pls help

$(document).on('contextmenu', '#txstory', function(e){
    e.preventDefault();
  var borderChars = ['\n', '\r', '\t'];
    var tex = $(this).val();
    var start = $(this)[0].selectionStart;
    var end = $(this)[0].selectionEnd;
    while (start > 0){
        if(borderChars.indexOf(tex[start]) == -1){--start;}
        else{break;}                        
    }
    while(end < tex.length){
        if(borderChars.indexOf(tex[end]) == -1){++end;}
        else{break;}
    }
    var str = tex.substr(start, end - start).trim();
  console.log(str);
});

$(document).on('contextmenu', '#ed', function(e){
    e.preventDefault();
    var borderChars = ['\n', '\r', '\t'];
    var tex = $(this).text();
    var start = $(this)[0].selectionStart;
    var end = $(this)[0].selectionEnd;
    while (start > 0){
        if(borderChars.indexOf(tex[start]) == -1){--start;}
        else{break;}                        
    }
    while(end < tex.length){
        if(borderChars.indexOf(tex[end]) == -1){++end;}
        else{break;}
    }
    var str = tex.substr(start, end - start).trim();
    console.log(str);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class='btx' id='txstory'>lorem ipsum</textarea>
<div class='ed' id='ed' contenteditable>dolor sit</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Get the user's selection's baseOffset, then loop through each line and check whether the offset is within the range of the index of the start of the line and index of the end of the line:

$(document).on('contextmenu', '#txstory', function(e){
    e.preventDefault();
  var borderChars = ['\n', '\r', '\t'];
    var tex = $(this).val();
    var start = $(this)[0].selectionStart;
    var end = $(this)[0].selectionEnd;
    while (start > 0){
        if(borderChars.indexOf(tex[start]) == -1){--start;}
        else{break;}                        
    }
    while(end < tex.length){
        if(borderChars.indexOf(tex[end]) == -1){++end;}
        else{break;}
    }
    var str = tex.substr(start, end - start).trim();
  console.log(str);
});

$(document).on('contextmenu', '#ed', function(e){
  var pos = document.getSelection().baseOffset;
  var lines = e.target.textContent.split("\n")
  var line;
  var characs = 0;
  for(let i = 0; i < lines.length; i++){
    if(characs <= pos && characs + lines[i].length >= pos){
      line = lines[i];
      break;
    }
    characs += lines[i].length
  }
  console.log(line)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class='btx' id='txstory'>lorem ipsum</textarea>
<div class='ed' id='ed' contenteditable>dolor sit</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