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

172
Visualizações
how to replace text from a textbox selecting option from contextmenu

How to replace the highlighted text in text area to something of choice through context menu?

I am using Chrome APIs to manipulate the context menus. Since it is not easily runnable on SO, I have changed it to trigger on pressing Enter.

// context menu code
/*
chrome.contextMenus.create({
  title: "tests", 
  contexts:["selection"], 
  onclick: scrap
});
*/

const scrap = () => {
  text = window.getSelection().toString();
  console.log(text);

}

document.getElementById('test').addEventListener("keypress", () => {
  if (event.key === "Enter") {
    event.preventDefault();
    scrap();
  }
});
<input type="text" id="test" name="test">

EDIT

how to get this working by selecting an option from context menu?

document.getElementById('test').addEventListener("keypress", () => {
  if (event.key === "Enter") {
    event.preventDefault();
    scrap();
  }
});



function scrap() 
{
console.log("test");



var txtarea = document.getElementById("test");

var allText = txtarea.value;

console.log(allText);//works
var start = txtarea.selectionStart;
var finish = txtarea.selectionEnd;
var allText = txtarea.value;
var newText=allText.substring(0, start)+"mytextreplacemnt"+allText.substring(finish, allText.length);

txtarea.value=newText;
console.log(newText);
}
<input type="text" id="test" name="test">

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