Paladini
Paladini

Reputation: 4572

Bold in the input causes weirdness text

I'm trying to implement (just for fun) a input text like Facebook do with their text area, I mean, the hashtag highlighter of Facebook). I'm following this question to do this.

I'm having some issues because I don't have the same css of this question, so I want know if you can help me solving this issue. here is the "weirdness" that I'm talking about:

yes, it's some "weirdness"

Here is my code:

// HTML
<div class="overlap" contenteditable='true'> </div>
<input type='text' class='medium-input' autocomplete='off'/> 

// JS
$('.medium-input').keyup(function (e) {
    var str =$('.medium-input').val();
    str = str.replace(/\n/g, '<br>');
    str = str.replace(/#([a-zA-Z0-9]+)/g, "<b>#$1</b>");
    $('.overlap').html(str);
});

// CSS
.medium-input, .overlap{
    width: 560px;
    position: relative;
    float: left;
    background-color: transparent;
    outline: 0;
    resize: none;
    direction: ltr;
}

.overlap{
    position:absolute;
    color:transparent;
    white-space: pre-wrap;
    max-width: 100%;
    height: 30px;
    padding: 7px 8px;
    font-family: sans-serif;
}

.overlap b{
    font-weight:bold;
    color:#333;
    display: inline-block;
    white-space: pre-wrap;
    word-wrap:break-word;
    direction: ltr; 
    text-align: left;
    max-width: 100%;
}

As you see, I don't know a lot of CSS and JS, I'm trying to learn by myself, but I'm having this issue, hope you can help me.

Upvotes: 1

Views: 135

Answers (1)

CronosS
CronosS

Reputation: 3159

Update

(see comment)

The simplest solution is to use a textarea (with no-resize). Then auto-expand it with a bit of javascript :

Updated Fiddle / Javascript:

$(function () {
    var overlap = $('.overlap'),
        input = $('.medium-input');
    input.on('input', function () {
        overlap.html(this.value.replace(/#([a-zA-Z0-9]+)/g, "<b>#$1</b>"));
        input.height(overlap.height());
    });
});



It's kinda complicated to work with bold text. It takes up more space, creating that "weirdness".
You can simulate a bold text by using a text-shadow:

jsFiddle Demo / CSS:

.medium-input, .overlap {
    position: relative;
    width: 560px;
    padding: 5px;
    background-color: transparent;
    border: 1px solid #999;
    outline: 0;
    font-size: 13px;
    font-family: sans-serif;
    word-spacing: 2px;
    color: #333;
    margin: 0;
}
.overlap {
    position: absolute;
    color: transparent;
    border: 1px solid transparent;
}
.overlap b {
    font-weight: normal;
    text-shadow: -1px 0 0 #666;
}

Or you can simply use another styling method:

jsFiddle Demo / CSS:

.medium-input, .overlap {
    position: relative;
    width: 560px;
    padding: 5px;
    background-color: transparent;
    border: 1px solid #999;
    outline: 0;
    font-size: 13px;
    font-family: sans-serif;
    word-spacing: 2px;
    color: #333;
    margin: 0;
}
.overlap {
    position: absolute;
    color: transparent;
    border: 1px solid transparent;
}
.overlap b {
    font-weight: normal;
    background-color: #dedede;
    padding: 0 3px;
    margin: 0 -3px 0 -3px;
    border-radius: 3px;
    box-shadow: 0 0 0 1px #999;
}

Upvotes: 1

Related Questions