Question

I have a problem, I would like to be able to make a thing, with Code Behind (also with JavaScript will be okay) . I have a textbox and a button and I would make this thing.

For example an user has written: Hello world, how are you? Now, he highlight with cursor "how are" and he press the button that will put an extra text (for example: bla) before and after the highlighted text. So, when the user press the button, on textbox he will have: Hello world, bla how are bla you?

Can someone help me? I thought that there was an option with System.windows.forms, but I have not found it.

Thank you so much!

Was it helpful?

Solution

Here is the solution

Javascript

function wraptext() {
    var SelectionStart = document.getElementById("text").selectionStart;
    var SelectionEnd = document.getElementById("text").selectionEnd;

    var OldVal = document.getElementById("text").value;
    var NewVal = OldVal.substring(0, SelectionStart) + " bla " + OldVal.substring(SelectionStart, SelectionEnd) + " bla " + OldVal.substring(SelectionEnd, OldVal.length);

    document.getElementById("text").value = NewVal;
}

Html

<input type="text" id="text" value="123456" />
<input type="button" value="Click Me" id="btn" onclick="wraptext();" />

Check demo here http://jsfiddle.net/iamrmin/P7gr8/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top