3

I'm trying to replicate the functionality of facebook's status update where tags within the post are highlighted. E.g, you hit the '@' character, and autocomplete to a page you are a fan of, and then that tagged piece of the text is highlighted even as you continue to type.

After digging into the dom, it looks like they pull this off similar to the technique recommended by this previous answer: overlaying an absolutely positioned div atop a text area with the tag surrounded by <b> and css to highlight the <b> tags within the overlay. A crucial tweak they add is to use color:transparent on the overlay so that only the highlight shows up.

alt text

This avoids the ugly darkening of the text being written on top of itself. Notice that without this rule (when I disable it in the dom inspector in chrome), facebook's update bar has the double text effect:

alt text

So far so good, but what I'm running into now is that color:transparent isn't supported in ie, so the ugly double text effect is there. I looked at the status update box in ie8 and facebook seems to get around this, but the developer tools aren't powerful enough to inspect the dom and see what they are doing (css inspection seems to be broken for that page).

Here's what I have so far, looking good in chrome:

alt text

and bad with the double text in IE8:

alt text

Any ideas? And please, keep suggestions to the specific overlay technique, I know I could try to use some big honking iframe embedding rich text editor, but this is really a simple enhancement that is only going to apply to a couple lines of text at most.

Full code below.

html:

        <div class="textarea textareaBorder">
            <textarea id="myTextarea" class="textarea realTextarea"></textarea>
            <div id="myOtherTextarea" class="textarea overlayTextarea"></div>
        </div>

css:

.textarea {
    font-family:monospace;
    font-size: 12px;
    border: 0;
    width: 100%;
    height: 200px;
}

.realTextarea {
    margin: 0;
    background: transparent;
    position: absolute;
    z-index: 999;
}

.overlayTextarea {
    margin: 0;
    position: absolute;
    color:transparent;
    top: 1px;
    left: 1px;
    z-index: 998;
}

.overlayTextarea b {
    background:#add8e6;
}

.textareaBorder {
    border: groove 1px #ccc;
    position: relative;
    width: 702px;
    height: 202px;
}

javascript:

$("textarea.realTextarea").keyup(function(e) {
    var textval = $(e.target).val();
    var markedup = textval.replace(/(the magic word)/g, "<b>$1</b>")

    $("#myOtherTextarea").html(markedup);
});
Community
  • 1
  • 1
Karl Rosaen
  • 4,508
  • 2
  • 27
  • 30

2 Answers2

2

To work around the lack of transparent color on IE, you can put the highlighted text and its surroundings inside additional <span> elements, force their opacity to zero using a filter, and keep the background color on the <b> element.

Of course, the <span> elements need to have layout for the filter to work, and setting their display property to inline-block will mess with whitespace, so you need to compensate for that by setting their white-space property to pre.

CSS:

.overlayTextarea b {
    background: #add8e6;
}

.overlayTextarea span {
    filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);
    display: inline-block;
    white-space: pre;
}

Javascript:

$("textarea.realTextarea").keyup(function(e) {
    var textval = $(e.target).val();
    var markedup = textval.replace(/(.*)(the magic word)(.*)/g,
        "<span>$1</span><b><span>$2</span></b><span>$3</span>");
    $("#myOtherTextarea").html(markedup);
});
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • Thanks for the answer, but using the filter makes the entire span -- including the background color -- transparent, so there is no highlighting effect. The trick seems to be in making *only* the text transparent while leaving the background color. – Karl Rosaen Nov 06 '10 at 13:58
  • @Karl, strange, the `background` property applies to your `` element, so it should still show through the transparent ``... Does it work better if you add `background-color: transparent;` to the `overlayText` class? – Frédéric Hamidi Nov 06 '10 at 14:02
  • hmm, going back to your original suggestion, you mention putting the highlighted portion in a span and setting its opacity to 0, but overall, we want the text color of the overlay to have opacity 0, and only the `` elements to have background color. – Karl Rosaen Nov 06 '10 at 14:15
  • so I interpreted your suggestion as simply trying to use the filter as a replacement for color:transparent for ie. I also tried putting a span inside the div. But that doesn't seem to work either. – Karl Rosaen Nov 06 '10 at 14:17
  • Yes, that's what I was suggesting :) I edited my answer and tried to make it clearer. – Frédéric Hamidi Nov 06 '10 at 14:18
  • wouldn't that leave the rest of the overlay, the non `` elements, opaque and thus having the double text artifacts? – Karl Rosaen Nov 06 '10 at 14:23
  • You're absolutely right. I'll try to get something to work by changing the regex and get back to you. – Frédéric Hamidi Nov 06 '10 at 14:26
  • thanks Frederic! I tried taking your suggestion, but also applying the filter along with `color:transparent` to to the `.overlayTextarea` div, and it still works in chrome, and doesn't have the double text effect in ie, but alas, the highlight doesn't come through. – Karl Rosaen Nov 06 '10 at 14:31
  • Got it. We need to use several `` elements. Answer updated. – Frédéric Hamidi Nov 06 '10 at 14:45
  • Thanks Frédéric, I can see how this makes more sense, but it simply seems that the filter is not taking effect when applied to the span. Have you tried this in ie and verified it works for you? I wonder if I'm still missing something. If so, perhaps you could past the complete example, css, js and html, in your answer. – Karl Rosaen Nov 06 '10 at 16:07
  • 1
    I finally gave up guessing, fired a VM and got the overlay working, under IE8 at least. See my edited answer. – Frédéric Hamidi Nov 06 '10 at 16:54
  • 1
    Frédéric, thanks so much for all the help! I think the inline-block was the missing piece of the puzzle. – Karl Rosaen Nov 08 '10 at 03:07
2

For posterity I wanted to post the solution I've settled on, which is a little bit cleaner than Frédéric's but inspired by the key breakthrough he laid forth.

Improvements:

  • only need to wrap the tokens in divs instead of all pieces of the text. this is done by having color:white for the text on the overlay that isn't highlighted instead of worrying about making it transparent.
  • add nbsp& along with spaces to make up for the fact that whitespace:pre breaks in ie after html is added to the echoing overlay div

css:

.annotated, .highlight, .inputtext, .inputtext textarea, .highlight .echoer {
    width:100%;
    height:30px;
}

.annotated {
    position:relative;
    border:1px solid black;
    height:40px;
}

.annotated .wrap {
    padding: 5px;
}

.annotated .highlight, .annotated .inputtext {
    position: absolute;
}

.annotated textarea {
    z-index:999;
    border:none;
    padding:0;
    margin:0;
    font-size:13px;
    white-space: pre-wrap;
    font-family: verdanna, arial, helvetica, sans-serif;
    background: transparent;
    overflow:hidden;
}

.annotated .echoer {
    z-index:998;
    border: none;
    padding:0;
    margin:0;
    font-size:13px;
    white-space: pre-wrap;
    font-family: verdanna, arial, helvetica, sans-serif;
    color:white;
}

.annotated .echoer b span {
    display:inline-block;
    filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0);
    color:transparent;
}

.annotated .echoer b{
    z-index:998;
    font-weight:normal;
    display:inline-block;
    background: #90ee90;
}

html:

        <div class="annotated">
            <div class="highlight">
                <div class="wrap">
                    <div class="echoer">

                    </div>
                </div>
            </div>

            <div class="inputtext">
                <div class="wrap">
                    <textarea>

                    </textarea>
                </div>
            </div>
        </div>

javascript:

$(".annotated textarea").keyup(function(e) {
    var textval = $(e.target).val();
    var markedup = textval.replace(/(the magic word)/g, "<b><span>$1</span></b>");

    // make up for the fact that ie loses the whitespace:pre after setting html
    markedup = markedup.replace(/ {2}/g, '&nbsp; ');
    $(".annotated .echoer").html(markedup);
Karl Rosaen
  • 4,508
  • 2
  • 27
  • 30