0

Is it possible to diversify the font size only in some parts of the text in a textarea with Javascript?

custom_button.onclick = function () {
    // IE version
    if (document.selection != undefined)
    {
        component_name.focus();
        var sel_ie = document.selection.createRange();
        textNew = sel_ie.text;
    }
    // Mozilla version
    else
    {
        var startPos = document.getElementById(component_name).selectionStart;
        var endPos = document.getElementById(component_name).selectionEnd;
        textNew = document.getElementById(component_name).value.substring(startPos, endPos);
    }
    var finalText = text.substr(0,startPos) + '<font size="100">' + textNew + '</font>' + text.substr(endPos);
}

When I usually write the code above I achieve the following result into the textarea:

This <font size="100">is the</font> text
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Pedrag
  • 15
  • 4
  • No, it's not possible to style just parts of the text in an input or textarea – adeneo Dec 25 '15 at 13:23
  • 1
    Not even at Christmas, @adeneo? – Andy Dec 25 '15 at 13:26
  • @Andy - If you ask Santa nicely, it can be done with more jQuery – adeneo Dec 25 '15 at 13:29
  • You'll have to make use of a `div` instead of using a `textarea` to achieve that. checkout `ckeditor` or `tinymce` libraries if you need more formatting options. – Sachin Dec 25 '15 at 13:30
  • 3
    Consider using a `div` with `contentEditable=true` if you're targeting IE9+, see http://stackoverflow.com/a/8957518/2741678 – Sumit Dec 25 '15 at 13:31
  • Possible duplicate of [Rendering HTML inside textarea](http://stackoverflow.com/questions/4705848/rendering-html-inside-textarea) – Sumit Dec 26 '15 at 15:16

0 Answers0