1

Let's say we have an input field: <input type="text" id="id0"/>. I would like to dynamically replace the current selection of the text with my text. What is the best way to achieve that using jQuery or javascript?

Update:

I meant: the user types the text and selects part of it, then presses a button to replace the selected section with another specified text.

Regards, Rafal

3
  • so, you need to select some text and find out what that is - replace it with some other text... in an input box? Commented Feb 14, 2012 at 11:55
  • when and to what text you want to replace? any event needs to be triggered ?? it would be helpful if i get you html and js code Commented Feb 14, 2012 at 11:56
  • Are you replacing on a button press or something? How will it know when to do the replace? Commented Feb 14, 2012 at 12:00

5 Answers 5

1

Most browsers support selectionStart and selectionEnd properties on text inputs and textareas. However, IE < 9 does not. You could use my Rangy inputs plug-in for this, which normalizes the mess of IE < 9's lack of support and provides convenient methods for dealing with selections in text inputs and textareas. Using it, the code would be:

$("#id0").replaceSelectedText("SOME NEW TEXT");

If the text box does not have the focus, you'll need to focus it first:

$("#id0").focus();
Sign up to request clarification or add additional context in comments.

5 Comments

That plugin uses document.execCommand('insertText') to replace the content (which is nice, as it correctly supports the browsers undo mechanic) but that doesn't work in IE11.
@flu: Yes, I know. There's a fallback for IE. However, it does mean the undo stack is broken in IE.
You're right. I'm just about to open a ticket for that undo bug.
@flu In the IE bug tracker or the plugin?
In the IE bug tracker. Currently when an input got a value Previous and you programatically set to New (via .value or jQuery.val()) and then hit CTRL+Z or choose undo from the context menu, the value will be changed to NewPrevious.
1
$('#id0').val('somenewtexthere');

1 Comment

Mike, I would like to dynamically replace the current selection, not the whole input value.
0

I'm not sure if this helps you but maybe in this way:

  var myMessage="My Text";
  $('#id0').val(myMessage);

Comments

0

If you want to clear it when you type and return its text when going away use this

$('#id0').focus(function() { $(this).val(''); });

$('#id0').blur(function() {$(this).val('  Your text    '); });  

Comments

-3

jQuery("input#id0").attr("value","my text");

4 Comments

-1 because of using jQuery instead of shorthand + using .attr() because there is a function that provides more functionality .val() would be the proper way.
There are some frameworks where you have to use jQuery instead of $ and I used another version (attr) because there was somebody else that gave an answer with val. My response was right and rises no error. I think you were mean by giving me -1, I was trying to help here.
Using .attr() is not good for this. See here why : stackoverflow.com/a/3843317/851498 . This would cause, for example, some form values to be not changed when submitted (attr() changes some jQuery property, while val() changes the DOM element value). I don't care about the jQuery :).
yeah in some projects but here we are giving answers that are up to date and which are not using any functions or habits that are either bad practice or deprecated. so the -1 was right to give.

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.