Reputation: 91
Is it possible to get the value of a text input and replace part of it only?
For example, let's say I have:
$("#myTextInput").val("hello sunshine!");
And I would like to replace "hello" with "good morning" WITHOUT erasing "sunshine"... Is that possible? Any help is greatly appreciated, thank you in advance!
Upvotes: 1
Views: 43
Reputation: 115222
You can use val()
with callback and update based on old value
$("#myTextInput").val(function(index, oldVal) {
return oldVal.replace('hello', 'good morning');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="myTextInput" value="hello abc" />
Upvotes: 2