Home > Software engineering >  Issues replacing dashes by <br> with javascript using wordpress
Issues replacing dashes by <br> with javascript using wordpress

Time:11-15

I am trying to replace all dashes(-) by a "br" but it gives me issues.

I have tried

var strNewString = $('.member__designation').html().replace(/-/g,'<br>');
$('.member__designation').html(strNewString);

Here is the html

Before applying the code above

After applying the code above

CodePudding user response:

You likely want

 $('.member__designation').each(function() {
   this.textContent = this.textContent.replace(/-/g,'<br>')
 })

assuming $('.member__designation') only has text and not html

IF there is only SIMPLE HTML like <b> then do

 $('.member__designation').each(function() {
   this.innerHTML = this.innerHTML.replace(/-/g,'<br>')
 })

CodePudding user response:

It looks like you grab all elements with class .member__designation and you replace them with whatever your regex grabs. I think you must change your implementation

  • Related