How to replace a string in Jquery or Javascript

Asked

Viewed 1,300 times

0

I have the message "- Do you really want to transfer the questions from..."

How do I remove (replace) from the beginning of a message the dash "-" ?

Follow the function that prints the message:

                        function closePopup11110() {
                            $("[id$='textoMensagem11110']").html('#{msg.MN067}');
                            $(".dialog11110").dialog('close');
                        }

2 answers

2


To replace using replace(), just pass the value you want to be replaced by a new value or none, if you also want to remove the space that is at the beginning of the text you can use the method Trim():

let texto = document.getElementsByTagName('p')[0].textContent;   // pega o texto
let novo = texto.replace('-', '');                               // substiui - por vazio

console.log(texto);                                              // texto original
console.log(novo);                                               // texto novo
console.log(novo.trim());                                        // sem espaço
<p>- Deseja realmente transferir as perguntas do...</p>

  • 1

    Leandrade, I applied your solution and it worked perfectly. Thanks for the support!

  • Cool man, I’ll be right there!

0

You can remove only the first character using substring:

let string = $("[id$='textoMensagem11110']").text();
string = string.substring(1);
$("[id$='textoMensagem11110']").html(string);

It is worth mentioning that the method text will read all the contents of your element, so you must change the selector to pick only the correct message.

  • I understand @Ayrton, but when do I replace to remove the dash at the beginning of the phrase "- Do you really want to transfer the questions from..."?

  • Before transplanting it to html (that would be line 3 of the code I put)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.