In a portion of my code to get a "report view" where the element is different (textarea becomes a div or span) and use the same data with newlines I replace the new lines with page breaks.
Currently the function I use:
$.each($('[id^="mytext-text"]'),(i,e)=>{$(e).html($(e).html().replace(/\n/gm,'<br>'))})
I often find myself using this same format for replacing other things, i.e.,
$(e).text($(e).text+'more text');
Is there a better way (either about using line breaks or about the .text(.text())
? I've seen suggestions to simply write your own function but I feel like that doesn't solve the generalized problem.
.text()
and.html()
can be a function. It receives the old text/html as an argument, and returns the replacement. You don't need.each()
, since it automatically loops, and calls the function for each element. \$\endgroup\$$('[id^="mytext-text"]').text((i,e)=>e.replace(/\n/gm,'<br>'))
! \$\endgroup\$(e) =>
. The first argument is the old text. \$\endgroup\$