ItSolutionStuff.com

How to Convert Line Breaks to br in jQuery ?

By Hardik Savani • November 5, 2023
Javascript 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..

Tags: jQuery
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

File Upload with Progress Bar using jQuery Ajax and PHP Example

Read Now →

How to Add JQuery Modal Popup in PHP?

Read Now →

How to Check Image Loaded or Not in JQuery?

Read Now →

Display Loading Image when AJAX Call is in Progress Example

Read Now →

How to Parse JSON in JQuery Ajax?

Read Now →

How to Get Max Attribute Value in JQuery?

Read Now →

Automatically Scroll to Bottom of Div JQuery Example

Read Now →

Check and Uncheck All Checkbox using JQuery Example

Read Now →

How to access PHP variables in JQuery?

Read Now →

How to Allow Only One Checkbox Checked at a Time in JQuery?

Read Now →

How to Check Object is Empty or Not in JQuery?

Read Now →

Autocomplete with Images and Custom HTML Code in Jquery UI?

Read Now →