문제

I fail to capitalize selected text in textareas via onClick.

After hours of googling I fopund out there is no built in method to get the select text in texareas.

I found Replacing selected text in the textarea which replaces text.

How could it be adjusted capitalize the selected text?

Testing: http://jsfiddle.net/tDYe4/2/

도움이 되었습니까?

해결책

Here's a modified version of your function that works. Updated your fiddle.

function capitalizeSelectedText(el) {
    var sel       = getInputSelection(el),
        val       = el.value,
        selection = val.substring(sel.start, sel.end);

    if(!selection) return;

    var new_text = val.substring(0, sel.start) + selection.toUpperCase() + val.substring(sel.end);
    el.value = new_text;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top