skip to Main Content

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

2

Answers


  1. 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>')
     })
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search