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

630
Visualizações
click event not working inside div when v-html used - vue js

I have one contenteditable div and adding raw html including a click event using v-html. But the click event is not working in this case. Inspecting elements shows there's a click event on the div but still it won't work.

If I hard code any other div with click event inside contenteditable div then it's working. Don't know if I am doing it right. :

<div id="div_textarea2" v-html="temp_testing_div2" contenteditable="true"></div>


   data () {
    return {
          temp_testing_div2: `<div @click="ClickLine">Click this</div>`,

}
}


   ClickLine(){
   alert("Clicked");
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

@HermantSah v-html won't bind any Vue directives such as @click events. See docs here.

A workaround could be to have the @click event on the parent div and in your template string give the element something distinguishable, like an id.
You could then intercept the event and check which element was clicked. If the element matches the one in your template string do something etc. A rough example below.

<div id="div_textarea2" v-html="temp_testing_div2" @click="handleLineClick" contenteditable="true"></div>

...  
// in your script section
data() {
  return {
    temp_testing_div2: `<div id="temp_testing_div2">Click this</div>`,
  }
},
methods: {
  handleLineClick(e) {
    let clickedElId = e.target.id
    if (clickedElId === 'temp_testing_div2') {
      console.log("temp_testing_div2 clicked!", e)
    } else {
      console.log('another element was clicked')
    }
  }
}

It all feels a little messy though in my opinion. There may be an easier way of achieving what you desire.

over 4 years ago · Santiago Trujillo 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