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(" <" + 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 > ?
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.