How to Convert Line Breaks to br in jQuery ?
If you are use textarea and you want to like break into br(\n) so that way we can give format as user insert, but if you are working on php then you can easily doing with nl3br() through, but if you are using in jquery or javascript then you have to first declare that. i did give full example of jquery textarea line breaks as under.
<body>
<textarea class="textareac"></textarea>
<script>
var text_content = <?php echo $content ?>;
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br/>' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
$(".textareac").val(nl2br(text_content));
</script>
<body/>
Try this..
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- File Upload with Progress Bar using jQuery Ajax and PHP Example
- How to Add JQuery Modal Popup in PHP?
- How to Check Image Loaded or Not in JQuery?
- Display Loading Image when AJAX Call is in Progress Example
- How to Parse JSON in JQuery Ajax?
- How to Get Max Attribute Value in JQuery?
- Automatically Scroll to Bottom of Div JQuery Example
- Check and Uncheck All Checkbox using JQuery Example
- How to access PHP variables in JQuery?
- How to Allow Only One Checkbox Checked at a Time in JQuery?
- How to Check Object is Empty or Not in JQuery?
- Autocomplete with Images and Custom HTML Code in Jquery UI?