How to remove the " n" with jQuery?

Asked

Viewed 39 times

1

I have a variable that receives the following content:

dadosP.formapgto = $('input[name=opcaoPagamento]:checked')
  .parent().text();

But I get some \n inside that string, as follows

Imagem com /n

Which also interferes with the final result:

resultado final

1 answer

3


You don’t need to use jQuery for this. Use the method String.prototype.trim, of Javascript itself.

For example:

const str = '       \n            \n pix\n';

// Remove os espaços em branco.
const result = str.trim();

console.log({ str, result });

jQuery has the function $.trim, that removes whitespace from the beginning and end of a string, as well as the method trim of the JS. However, as we have seen above, since Javascript has natively method with this functionality, this function has been deprecated and is no longer encouraged by jQuery’s own authors.

In short, it would be enough to do:

$('input[name=opcaoPagamento]:checked')
  .parent().text().trim();
  • Thank you very much, it went super well, thank you very friend! :)

Browser other questions tagged

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