开发者

How do you replace content without losing focus?

开发者 https://www.devze.com 2022-12-28 10:38 出处:网络
For example: <input type=\"text\" name=\"test\" onChange=\"document.formname.test.value=.document开发者_StackOverflow中文版.formname.test.value.replace(\'something\',\'something else\')\" />

For example:

<input type="text" name="test" onChange="document.formname.test.value=.document开发者_StackOverflow中文版.formname.test.value.replace('something','something else')" />

The replace function works but it loses focus on every change

How do you make it not lose focus?

What I'm trying to do is make it so that certain text is immediately replaced with new text when its typed but you can continue typing


Short answer: document.formname.test.focus();

<input type="text" name="test" onChange="document.formname.test.value=document.formname.test.value.replace('something','something else'); document.formname.test.focus();" />


If you want to maintain focus on that input try:

onChange="document.formname.test.value=.document.formname.test.value.replace('something','something else'); document.formname.test.focus();"


Please do not use the onChange attribute javascript should never be in the html markup endless absolutely necessary. instead include your script at the end of the page body.

<body>
    some content...
    <script src="path to your script" type="text/javascript"></script>
</body>
0

精彩评论

暂无评论...
验证码 换一张
取 消