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 7746499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:17:38+00:00 2026-06-01T10:17:38+00:00

I have found various examples of using jquery to wrap selected text from a

  • 0

I have found various examples of using jquery to wrap selected text from a textarea in html tags, but I would like to adapt this slightly to create a list when multiple lines of text are selected.

Currently the code below wraps the whole selection in the list tags but I would also like to replace all the carriage returns with the closing and opening tags for list items – so each line in the text area is a new list item.

I think one of the problems might be that the .val function reads the text area as a single line.

jquery:

function listText(elementID, openTag, closeTag) {
    var textArea = $('#' + elementID);
    var len = textArea.val().length;
    var start = textArea[0].selectionStart;
    var end = textArea[0].selectionEnd;
    var selectedText = textArea.val().substring(start, end);
    var replacement = openTag + selectedText + closeTag;
    textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
}

$(document).ready(function () {
    $("#BoldIt").click( function() {
        listText("markItUp", "<ul><li>", "</li></ul>");
    });
});

body:

<textarea id="markItUp" cols="80" rows="20"></textarea>

<br />

<input type="button" value="Bold" id="BoldIt" />
  • 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-06-01T10:17:39+00:00Added an answer on June 1, 2026 at 10:17 am

    split your text like this:

    function listText(elementID, openTag, closeTag) {
        var textArea = $('#' + elementID);
        var s = "\n";
        var len = textArea.val().length;
        var start = textArea[0].selectionStart;
        var end = textArea[0].selectionEnd;
        var selectedText = textArea.val().substring(start, end);
        var replacement = "";
    
        var rows = selectedText.substring(start, end).split(s);
    
        for(var i = 0; i < rows.length; i++) {
        replacement += openTag + rows[i] + closeTag + s;
        }
    
        textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
    }
    
    $(document).ready(function () {
        $("#BoldIt").click( function() {
        listText("markItUp", "<ul><li>", "</li></ul>");
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have done various tests and I have found that the jQuery validate function
I have searched the documentation and tried various techniques but have't found a suitable
I have found the way to copy the record that I would like, but
I have an app built using jQuery (and using various jQuery-UI tools). For some
I am trying to display an image using purely javascript. I have found various
I have found numerous examples on uploading a file to a web server via
I have found the Form.TopMost property but it puts the form on top of
I have found various topics here and elsewhere on creating an intent for sending
I am trying to implement multibinding in Silverlight using VB.Net. I have found a
I've been using extension methods quite a bit recently and have found a lot

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.