Javascript: Append string in specific offset position of an input
Is there a way to append a string in a specific offset position on an input? Suppose we have the following input :
an offset : <input type="text" value="abcdef"/>
, the letter c is situated in the 3th offset of the input string value.
+-------------+
123456789 +-------------+ xxxxxxx->offset positionL开发者_如何学运维et's say I want to append the string "hi" in the 3th poistion of the input so it become :
+-------------+
hi +-------------+ 123456789How can I do that?
thanks.
You could very easily write your own function to do that.
function insertAt(src, index, str) {
return src.substr(0, index) + str + src.substr(index)
}
alert(insertAt("test", 2, "FOO")); //Alerts teFOOst
精彩评论