0

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!

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

4 Comments

@Rashmin: Your code uses jquery. Can you simplify it to make it usable in plain javascript?
Edit done, logic was already in core javascript. i uses jquery to bind click handler only, but now all code in core javascript
@RashminJaviya: Thanks. I think this should help OP.
@DanieleNekoLuciani in jsfiddle link select 2 character from text and click button

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.