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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:23:09+00:00 2026-05-20T09:23:09+00:00

I have a textarea and among free text input I insert some tags that

  • 0

I have a textarea and among free text input I insert some tags that are enclosed in <tag>. I’d like to be able to click on a tag (i.e.) within < > and select the whole tag in order to delete. the whole thing and not one character at a time.

<textarea>some text and <my tag> here</textarea>

I suppose I need to use some sort of regex check on click?


UPDATE:

OK, I’ll try to modify this question. I’m not doing it to make it more difficult, it’s just I keep brainstorming as well as I go.

Let’s say I have my tags predefined and I put them in my textarea by clicking a with corresponding ID:

var myTag1 = "my tag",
myTag2 = "another tag";

$("#tag1").text(myTag1);
$("#tag2").text(myTag2);

$(".tags").click(function () {
    var insertTag = $(this).text();
    $('#myTextArea').append(" &lt;" + insertTag + ">");
});

<div id="tag1" class="tags"></div>
<div id="tag2" class="tags"></div>

<textarea id="myTextArea"></textarea>

Now, let’s say I composed a message and inserted either both tag1 and tag2 or just one. Is it possible to click on a tag insert within that textarea and delete that tag as well as surrounding < and > ?

DEMO: http://jsfiddle.net/2K2CX/2/

  • 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-20T09:23:09+00:00Added an answer on May 20, 2026 at 9:23 am
    <textarea id="textarea" style="width:500px;height:200px;">Lorem ipsum dolor sit amet, consectetur <adipiscing> elit. Aliquam adipiscing feugiat vestibulum. <Proin risus massa>, cursus quis eleifend tristique, pharetra eget ligula. <Suspendisse potenti>. Etiam vel lectus ante. Fusce varius laoreet lobortis. Aliquam ornare dictum lacus, non pharetra mauris fringilla sit amet. Quisque accumsan viverra dui, non pellentesque arcu bibendum et. Sed vel odio in libero scelerisque posuere. Phasellus euismod leo non arcu vulputate in sollicitudin sem facilisis. Morbi et dui luctus sem molestie commodo ac vel justo.</textarea>
    <script type="text/javascript">
        var textarea = document.getElementById("textarea");
        textarea.onclick = textarea_Click;
    
        function textarea_Click() {
            var caret = getCaretPosition(textarea);
            var text = textarea.value;
            var begin = caret - 1;
            while (begin >= 0) {
                if (text.charAt(begin) == '>') return;
                else if (text.charAt(begin) == '<') break;
                else begin--;
            }
    
            if (begin >= 0) {
                var end = caret;
                while (end < text.length) {
                    if (text.charAt(end) == '>') break;
                    else end++;
                }
    
                if (end < text.length)
                    setSelection(textarea, begin, end);
            }
        }
    
        function getCaretPosition(el) {
            if (el.selectionStart) {
                return el.selectionStart;
            } else if (document.selection) {
                var r = document.selection.createRange();
                if (r == null) return 0;
                var re = el.createTextRange();
                var rc = re.duplicate();
                re.moveToBookmark(r.getBookmark());
                rc.setEndPoint('EndToStart', re);
                return rc.text.length;
            }
            return 0; 
        }
    
        function setSelection(el, begin, end) {
            if ("selectionStart" in el) {
                el.selectionStart = begin;
                el.selectionEnd = end + 1;
            } else if (document.selection) {
                var range = el.createTextRange();
                range.collapse(true);
                range.moveEnd('character', end + 1);
                range.moveStart('character', begin);
                range.select();
            }
        }
    </script>
    

    EDIT: Since you’re using jQuery, the fieldSelection plugin might make things a lot cleaner.

    As for a simple regex solution, nothing immediately comes to mind, but I’m also no regexpert 🙂

    EDITED AGAIN: I changed my original code so that it now supports IE. Tested in Firefox, Chrome, and IE 6/7/8.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have saved input from a textarea element to a TEXT column in MySQL.
i have a input tag which is non editable, but some times i need
I have a textarea with many lines of input, and a JavaScript event fires
Let's say I have an html form. Each input/select/textarea will have a corresponding <label>
I have a form with a textarea. Users enter a block of text which
I want to select every textarea that does not have a DIV with class
I'm developing an ajax heavy web app, and would like to have textarea 's
I have a textarea html element on my page that gets reloaded via ajax.
I have a textarea in a form that will eventually be an email form.
I have a page that contains multiple textarea elements. These textareas and the IDs

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.