Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6579521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:54:31+00:00 2026-05-25T15:54:31+00:00

Possible Duplicate: jQuery convert line breaks to br (nl2br equivalent) Currently I add <BR>

  • 0

Possible Duplicate:
jQuery convert line breaks to br (nl2br equivalent)

Currently I add <BR> for each evt.which == 13. Is there a nl2br() for JavaScript, so I can do away with this evt.which == 13?

How different is this from php.js

$('#TextArea').keypress(function(evt) {

    if (evt.which == 13) {

        var range           = $('#TextArea').getSelection();
        var image_selection = range.text;

        $('#TextArea').replaceSelection('<BR>');
        $('#TextArea1').html($('#TextArea').val());
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:54:32+00:00Added an answer on May 25, 2026 at 3:54 pm

    Take a look at nl2br on php.js which seems exactly what you’re looking for. Basically, it’s:

    function nl2br (str, is_xhtml) {
        if (typeof str === 'undefined' || str === null) {
            return '';
        }
        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');
    }
    

    EDIT:
    your example using nl2br() may be changed like this:

    $('#TextArea').keypress(function(evt){
            $('#TextArea1').html(nl2br($('#TextArea').val()));
        });
    

    (note that this updates #TextArea1 on every keypress and doesn’t change the value of #TextArea wich is what I think you’re looking for, but I might be misunderstanding)

    EDIT2:
    If you want to get the behaviour of your old function (with inserting <br/>s to #TextArea) do this:

    $('#TextArea').keypress(function(evt){
            $('#TextArea').html(nl2br($('#TextArea').val())); // replace linebreaks first
            $('#TextArea1').html($('#TextArea').val()); // copy to #TextArea1
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jQuery equivalent of JavaScript's addEventListener method Also from a very good jQuery
Possible Duplicate: javascript: pause setTimeout(); Im using jQuery and working on a notification system
Possible Duplicate: DOM Mutation event in JQuery or vanilla Javascript I am using backbone.js
Possible Duplicate: javascript (jquery) calendar plugin I am going to develop the weekly calendar
Possible Duplicate: select all checkboxes command jquery, javascript I am having trouble selecting all
Possible Duplicate: Javascript function to convert color names to hex codes Is it possible
Possible Duplicate: Jquery Add Values to Select Options How can I empty a dropdown,
Possible Duplicate: jQuery: How to determine which <li> tag was clicked? I have a
Possible Duplicate: Convert a Unix timestamp to time in Javascript I am getting date
Possible Duplicate: jQuery: how to add <li> in an existing <ul>? I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.