[Solved] adding a tag when a button is clicked [closed]


You can use selectionStart, selectionEnd properties of the textarea.

function getSel(){
    var txtarea = document.getElementById("mytextarea");
    var start = txtarea.selectionStart;
    var finish = txtarea.selectionEnd;
    txtarea.value =
        txtarea.value.substring(0, start) + 
        '<mytag>' + txtarea.value.substring(start, finish) + '</mytag>' +
        txtarea.value.substring(finish);
}

solved adding a tag when a button is clicked [closed]