jQuery - How do you convert <br>
and <br />
and <p />
and such to new line?
Does jQuery have a built in br2nl()
function - this is for converting new lines tags to user-friendly textfield versions.
You could create a function like this:
jQuery.fn.nl2br = function(){
return this.each(function(){
jQuery(this).val().replace(/(<br>)|(<br \/>)|(<p>)|(<\/p>)/g, "\r\n");
});
};
And use it like any of these ways:
$(':input').nl2br();
$('textarea').nl2br();
$('#textarea_id').nl2br();
If you want to take a chunk of html and replace
tags and self-closing tags with newline characters, doing something like:
$('#element-containing-html-to-replace').html().replace(/(<br>)|(<p><\/p>)/g, "\n");
should return a string of the container's HTML with those tags replaced with newline characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With