Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!