tags:

views:

53

answers:

3

For example:

<input type="text" name="test" onChange="document.formname.test.value=.document.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

A: 

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();"
ANC_Michael
+2  A: 

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();" />
Jonathan Czitkovics
A: 

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>
Robert Hurst
That comes at the expense of preventing the scripted behaviour from working until the whole page has loaded, meaning a user could interact with the form element before its `onchange` has been set. Also, this doesn't answer the question.
Tim Down