Set Focus to html form problem

H

hugo

Hi,

I have a function that I call from form fields using the OnKeyUp
function to replace special caracters. Once this function has been
called, it does not set the focus on the form field where I have
called the function from.

What happens :

I laod a EDIT page with a html form on it and fill in the fields with
values from a database. Now when I use the tab button to move to next
field that contains a value, it calls the function and then the focus
is not set to the same field, so I have to click on the field with the
mouse to edit the value.

Below is a example of my form and then the function in that it calls.

Any help will be much apreciated.

HTML FORM :

<input type=text name=Street Value="<% =Server.HTMLEncode(rs("Street"))
%>" size="30" onKeyUp="fix_chars(this);"><BR>
<input type=text name=Suburb Value="<% =Server.HTMLEncode(rs("Suburb"))
%>" size="30" onKeyUp="fix_chars(this);"><BR>
<input type=text name=Location Value="<%
=Server.HTMLEncode(rs("Location"))%>" size="30"
onKeyUp="fix_chars(this);"><BR>

JAVASCRIPT FUNCTION :

function fix_chars(textBox)
{
textBox.value = textBox.value.replace(/[À]/g, "A");
textBox.value = textBox.value.replace(/[Ã]/g, "A");
textBox.value = textBox.value.replace(/[Â]/g, "A");
textBox.value = textBox.value.replace(/[Ã]/g, "A");
textBox.value = textBox.value.replace(/[Ä]/g, "C");
textBox.value = textBox.value.replace(/[Ã…]/g, "C");
textBox.value = textBox.value.replace(/[Æ]/g, "AE");
textBox.value = textBox.value.replace(/[Ç]/g, "C");
textBox.value = textBox.value.replace(/[È]/g, "E");
textBox.value = textBox.value.replace(/[É]/g, "E");
textBox.value = textBox.value.replace(/[Ê]/g, "E");
textBox.value = textBox.value.replace(/[Ë]/g, "E");
textBox.value = textBox.value.replace(/[Ì]/g, "I");
textBox.value = textBox.value.replace(/[Ã]/g, "I");
textBox.value = textBox.value.replace(/[ÃŽ]/g, "ÃŽ");
textBox.value = textBox.value.replace(/[Ã]/g, "I");
textBox.value = textBox.value.replace(/[Ã]/g, "ETH");
textBox.value = textBox.value.replace(/[Ñ]/g, "N");
textBox.value = textBox.value.replace(/[Ã’]/g, "O");
textBox.value = textBox.value.replace(/[Ó]/g, "O");
textBox.value = textBox.value.replace(/[Ô]/g, "O");
textBox.value = textBox.value.replace(/[Õ]/g, "O");
textBox.value = textBox.value.replace(/[Ö]/g, "O");
textBox.value = textBox.value.replace(/[Ø]/g, "O");
textBox.value = textBox.value.replace(/[Ù]/g, "U");
textBox.value = textBox.value.replace(/[Ú]/g, "U");
textBox.value = textBox.value.replace(/[Û]/g, "U");
textBox.value = textBox.value.replace(/[Ü]/g, "U");
textBox.value = textBox.value.replace(/[Ã]/g, "Y");
textBox.value = textBox.value.replace(/[Þ]/g, "THORN");
textBox.value = textBox.value.replace(/[ß]/g, "s");
textBox.value = textBox.value.replace(/[à]/g, "a");
textBox.value = textBox.value.replace(/[á]/g, "a");
textBox.value = textBox.value.replace(/[â]/g, "a");
textBox.value = textBox.value.replace(/[ã]/g, "a");
textBox.value = textBox.value.replace(/[ä]/g, "a");
textBox.value = textBox.value.replace(/[Ã¥]/g, "a");
textBox.value = textBox.value.replace(/[æ]/g, "ae");
textBox.value = textBox.value.replace(/[ç]/g, "c");
textBox.value = textBox.value.replace(/[è]/g, "e");
textBox.value = textBox.value.replace(/[é]/g, "e");
textBox.value = textBox.value.replace(/[ê]/g, "e");
textBox.value = textBox.value.replace(/[ë]/g, "e");
textBox.value = textBox.value.replace(/[ì]/g, "i");
textBox.value = textBox.value.replace(/[í]/g, "i");
textBox.value = textBox.value.replace(/[î]/g, "i");
textBox.value = textBox.value.replace(/[ï]/g, "i");
textBox.value = textBox.value.replace(/[ð]/g, "eth");
textBox.value = textBox.value.replace(/[ñ]/g, "n");
textBox.value = textBox.value.replace(/[ò]/g, "o");
textBox.value = textBox.value.replace(/[ó]/g, "o");
textBox.value = textBox.value.replace(/[ô]/g, "o");
textBox.value = textBox.value.replace(/[õ]/g, "o");
textBox.value = textBox.value.replace(/[ö]/g, "o");
textBox.value = textBox.value.replace(/[ø]/g, "o");
textBox.value = textBox.value.replace(/[ù]/g, "u");
textBox.value = textBox.value.replace(/[ú]/g, "u");
textBox.value = textBox.value.replace(/[û]/g, "u");
textBox.value = textBox.value.replace(/[ü]/g, "u");
textBox.value = textBox.value.replace(/[ý]/g, "y");
textBox.value = textBox.value.replace(/[þ]/g, "thorn");
textBox.value = textBox.value.replace(/[ÿ]/g, "y");
textBox.value = textBox.value.replace(/[Å’]/g, "OE");
textBox.value = textBox.value.replace(/[Å“]/g, "oe");
textBox.value = textBox.value.replace(/[Å ]/g, "S");
textBox.value = textBox.value.replace(/[Å¡]/g, "s");
textBox.value = textBox.value.replace(/[Ÿ]/g, "Y");


}


THANKS Hugo
 
H

hugo

(e-mail address removed) said the following on 3/28/2007 9:52 AM:
I have a function that I call from form fields using the OnKeyUp
function to replace special caracters.

Having a page change my text as I type would be irritating at the least.
Use the onchange event instead.
Once this function has been called, it does not set the focus
on the form field where I have called the function from.

Set a global variable and every time a field gains focus, set that
variable to the fields name. Then at the end of your function set focus
back where you wanted it.

onfocus="thisField=this.name;"

var thisField="";
function fix_chars(textBox)
{

<snip>

document.forms['formID'].elements[thisField].focus();

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Thanks for your help dude !

I have tried the following but get an error.
I have changed ['formID'] to ['myform'] because my form name is
"myform" Is this correct ?

Any more help ?

<script language="javascript">
<!--

onfocus="thisField=this.name;"
var thisField="";


function fix_chars(textBox)
{

textBox.value = textBox.value.replace(/[À]/g, "A");
textBox.value = textBox.value.replace(/[Ã]/g, "A");
textBox.value = textBox.value.replace(/[Â]/g, "A");
textBox.value = textBox.value.replace(/[Ã]/g, "A");
textBox.value = textBox.value.replace(/[Ä]/g, "C");
textBox.value = textBox.value.replace(/[Ã…]/g, "C");
textBox.value = textBox.value.replace(/[Æ]/g, "AE");
textBox.value = textBox.value.replace(/[Ç]/g, "C");
textBox.value = textBox.value.replace(/[È]/g, "E");
textBox.value = textBox.value.replace(/[É]/g, "E");
textBox.value = textBox.value.replace(/[Ê]/g, "E");
textBox.value = textBox.value.replace(/[Ë]/g, "E");
textBox.value = textBox.value.replace(/[Ì]/g, "I");
textBox.value = textBox.value.replace(/[Ã]/g, "I");
textBox.value = textBox.value.replace(/[ÃŽ]/g, "ÃŽ");
textBox.value = textBox.value.replace(/[Ã]/g, "I");
textBox.value = textBox.value.replace(/[Ã]/g, "ETH");
textBox.value = textBox.value.replace(/[Ñ]/g, "N");
textBox.value = textBox.value.replace(/[Ã’]/g, "O");
textBox.value = textBox.value.replace(/[Ó]/g, "O");
textBox.value = textBox.value.replace(/[Ô]/g, "O");
textBox.value = textBox.value.replace(/[Õ]/g, "O");
textBox.value = textBox.value.replace(/[Ö]/g, "O");
textBox.value = textBox.value.replace(/[Ø]/g, "O");
textBox.value = textBox.value.replace(/[Ù]/g, "U");
textBox.value = textBox.value.replace(/[Ú]/g, "U");
textBox.value = textBox.value.replace(/[Û]/g, "U");
textBox.value = textBox.value.replace(/[Ü]/g, "U");
textBox.value = textBox.value.replace(/[Ã]/g, "Y");
textBox.value = textBox.value.replace(/[Þ]/g, "THORN");
textBox.value = textBox.value.replace(/[ß]/g, "s");
textBox.value = textBox.value.replace(/[à]/g, "a");
textBox.value = textBox.value.replace(/[á]/g, "a");
textBox.value = textBox.value.replace(/[â]/g, "a");
textBox.value = textBox.value.replace(/[ã]/g, "a");
textBox.value = textBox.value.replace(/[ä]/g, "a");
textBox.value = textBox.value.replace(/[Ã¥]/g, "a");
textBox.value = textBox.value.replace(/[æ]/g, "ae");
textBox.value = textBox.value.replace(/[ç]/g, "c");
textBox.value = textBox.value.replace(/[è]/g, "e");
textBox.value = textBox.value.replace(/[é]/g, "e");
textBox.value = textBox.value.replace(/[ê]/g, "e");
textBox.value = textBox.value.replace(/[ë]/g, "e");
textBox.value = textBox.value.replace(/[ì]/g, "i");
textBox.value = textBox.value.replace(/[í]/g, "i");
textBox.value = textBox.value.replace(/[î]/g, "i");
textBox.value = textBox.value.replace(/[ï]/g, "i");
textBox.value = textBox.value.replace(/[ð]/g, "eth");
textBox.value = textBox.value.replace(/[ñ]/g, "n");
textBox.value = textBox.value.replace(/[ò]/g, "o");
textBox.value = textBox.value.replace(/[ó]/g, "o");
textBox.value = textBox.value.replace(/[ô]/g, "o");
textBox.value = textBox.value.replace(/[õ]/g, "o");
textBox.value = textBox.value.replace(/[ö]/g, "o");
textBox.value = textBox.value.replace(/[ø]/g, "o");
textBox.value = textBox.value.replace(/[ù]/g, "u");
textBox.value = textBox.value.replace(/[ú]/g, "u");
textBox.value = textBox.value.replace(/[û]/g, "u");
textBox.value = textBox.value.replace(/[ü]/g, "u");
textBox.value = textBox.value.replace(/[ý]/g, "y");
textBox.value = textBox.value.replace(/[þ]/g, "thorn");
textBox.value = textBox.value.replace(/[ÿ]/g, "y");
textBox.value = textBox.value.replace(/[Å’]/g, "OE");
textBox.value = textBox.value.replace(/[Å“]/g, "oe");
textBox.value = textBox.value.replace(/[Å ]/g, "S");
textBox.value = textBox.value.replace(/[Å¡]/g, "s");
textBox.value = textBox.value.replace(/[Ÿ]/g, "Y");

document.forms['myform'].elements[thisField].focus();

}
-->
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top