ES6 template literals inside jQuery $ .each method

Can ES6 template literals be used inside a jQuery method $.each

?

Tried to do this without success:

let arr = this.arr;

  $.each($("g#texts").children(), function (i, contents) {
    $("#`${contents.id}` tspan")
        .text(arr.find(a => a.name == "`${contents.id}`")
        .displayedName);
  })

      

What needs to be fixed here?

+3
javascript jquery ecmascript-6


source to share


1 answer


This is certainly possible. The problem is that you have placed the template literal inside the string literal. The second template literal is also redundant. If you correct the syntax, the code you write will work fine:



$("g#texts").children().each(function (i, contents) {
  $(`#${contents.id} tspan`).text(arr.find(a => a.name == contents.id).displayedName);
});

      

+5


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics