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

108
Visualizações
How to select the closest element with contains selector jquery

I want to replace the "{{my_name}}" and "{{my_email}}" with a string, but the program is not working, what should i do?

Thanks.

$(".notes *:contains('{{')").text(function() {
  var rawkey = $(this).text().match(/{{(.*?)}}/i)[0]; // Output (console.log) : {{my_name}} or {{my_email}}
  var key = $(this).text().match(/{{(.*?)}}/i)[1]; // Output (console.log) : my_name or my_email

  if (key.indexOf('name') > -1) {
    $(this).text($(this).text().replace(rawkey, "Tony"));
  } else if (key.indexOf('email') > -1) {
    $(this).text($(this).text().replace(rawkey, "tonyhawk@gmail.com"));
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="notes">
  <h4>{{my_name}}</h4>
  <table>
    <tr>
      <td>
        <p>{{my_name}}, {{my_email}}</p>
      </td>
    </tr>
  </table>
</div>

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

0

Set the result after replacing back to the element's textContent:

$(".notes *:contains('{{')").text(function() {
    var rawkey = $(this).text().match(/{{(.*?)}}/i)[0];
    var key = $(this).text().match(/{{(.*?)}}/i)[1];

    if (key.indexOf('name') > -1) {
        $(this).text($(this).text().replace(rawkey, "Tony"));
    } else if (key.indexOf('email') > -1) {
        $(this).text($(this).text().replace(rawkey, "tonyhawk@gmail.com"));
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="notes">
  <h4>{{my_name}}</h4>
  <table>
    <tr>
      <td>
        <p>{{my_email}}</p>
      </td>
    </tr>
  </table>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do like this.

 var $content=$('.notes').html();
var finalContent=$content.replaceAll("my_name","Tony").replaceAll("my_email","tonyhawk@gmail.com").replaceAll("{{","").replaceAll("}}","");
$(".notes").html(finalContent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notes">
  <h4>{{my_name}}</h4>
  <table>
    <tr>
      <td>
        <p>{{my_name}},{{my_email}}</p>
      </td>
    </tr>
  </table>
</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