1

I am trying to figure out how facebook does the person-highlight tagging below.

I have a <textarea> and what I want is that ability when I press backspace the whole name gets deleted.

And I also wanted the name to be highlighted in blue (but this one is easy).

Can this be done easily through css? Or do I have to use some sort of javascript to have the deletion stuff working?

TLDR: I needed a jQuery function that deletes the name that is highlighted when I press back..that's all, no need for autocompletion and such

enter image description here

adit
  • 32,574
  • 72
  • 229
  • 373
  • CSS is used for styling, not for manipulation of data / events. You'll need javascript for that. On that note, yes, this is possible. Using Jquery and catching the keydown/keyup or keypress event, checking if its backspace, and removing until you hit a space should do it. Highlighting isn't hard either, having a class style, and adding that class to a span around the word, which you could also add with jquery, would probably suffice. – Stefan Candan May 16 '13 at 05:48

2 Answers2

1

You can use Jquery Token input https://github.com/loopj/jquery-tokeninput

More comprehensive list is here: Facebook style JQuery autocomplete plugin

Community
  • 1
  • 1
drhanlau
  • 2,517
  • 2
  • 24
  • 42
  • the plugin is a bit an overkill. I needed a jQuery function that deletes the name that is highlighted when I press back..that's all. No autocomplete – adit May 16 '13 at 05:54
  • 1
    yup, can't you just not doing the autocomplete and just use the delete token feature? – drhanlau May 16 '13 at 06:17
1

you can make it by adding an absolute div behind the required text, and make the background transparent for the textarea.
here is a snippet of code that I just wrote, it might help you.
I just faced some problems with adding the correct left position to the highlighted div.

html :

<div class='container'>
  <div class='highlighted'></div>
  <textarea class='text_area'>hi my name is Ayman</textarea>
</div>

css :

.custom_table{
position: relative;
width:600px;
}

.row{
position: relative;
height:40px;
background: #c80000;
border-top:1px solid #fff;
}

.row div{
color:#fff;
text-align: center;
line-height:40px;
}
.row:hover .first,
.row:hover .second,
.row:hover .third,
.row:hover .fourth{
background:#522D2D;;
cursor:pointer;
}

.first{
position: absolute;
left:0%;
right:80%;
height:40px;
background: #00c800;
}

.second{
position: absolute;
left:20%;
right:60%;
height:40px;
background: #0000c8;
}

.third{
position: absolute;
left: 40%;
right: 25%;
height: 40px;
background: #BEBECF;
}

.fourth{
position: absolute;
left: 75%;
right: 0%;
height: 40px;
background: #D6182F;
}

JavsScript :

$(document).ready(function(e){
$('.text_area').bind('keypress', function(e) {
     var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 64){
        $('.text_area').val($('.text_area').val()+" Ayman")
        line = $('.text_area').val().substr(0, $('.text_area').selectionStart).split("\n").length;
        var hl = $("<div class='highlighted'></div>");
        $(hl).css({'left':$('.text_area').getCursorPosition()+"px", 'top': ((line*14)-12)+"px"})
        $('.container').append($(hl))
    }
});
});

(function ($, undefined) {
$.fn.getCursorPosition = function() {
    var el = $(this).get(0);
    var pos = 0;
    if('selectionStart' in el) {
        pos = el.selectionStart;
    } else if('selection' in document) {
        el.focus();
        var Sel = document.selection.createRange();
        var SelLength = document.selection.createRange().text.length;
        Sel.moveStart('character', -el.value.length);
        pos = Sel.text.length - SelLength;
    }
    return pos;
}
})(jQuery);
Evan Lévesque
  • 3,115
  • 7
  • 40
  • 61