List Info

Thread: Why it focus is not working with this syntex




Why it focus is not working with this syntex
country flaguser name
United States
2007-06-15 15:08:00

Why this code is not working.
When i have clicked the 'click' button , i have shown a alert box and
after clicking that i want to focus the text field.

it is not working with the following code.

main.js--------------

function doFocus(myForm,myField)
{
alert('abcd');
//document.getElementById(myField).focus();
document.myForm.myField.focus();
}

index.php---------------

<html>;
<head>
<;SCRIPT language=JavaScript src="Main.js&quot;></SCRIPT>
</head>
<;body>
<form name=";field"; id="field"&gt;
<table width=&quot;800" border=&quot;1" height=&quot;500"; align=&quot;center&quot;>
<tr>
<td><INPUT type=";text" name=";pass" id="pass"&gt;</td&gt;
<tr>
<td><INPUT type=";button&quot; name=";bt1" id="bt1" onclick=&quot;doFocus
('field','pass');&quot; width=&quot;10" value=&quot;click&quot;></td>
</tr>;
</tr>;
</table>
&lt;/form>;
</body>
&lt;/html>

This code is not working with the syntex

document.myForm.myField.focus(); in main.js file.

but if i place this systex

document.getElementById(myField).focus();

then it is working,

help me.

__._,_.___
.

__,_._,___
Re: Why it focus is not working with this syntex
country flaguser name
United States
2007-06-15 18:57:00

document.myForm.myField is looking for a form with the actual name "myForm"
and an actual field "myField". It doesn't treat these items as variables in
the dot notation - more like properties.

Use getElementById to get the form, then use collections subscript notation
to get the field. Something like

var theForm = document.getElementById (myForm);
theForm (myField).focus ();

*** not tested - off the top of my head ***

Or go straight for the field with getElementById if its ID is unique on the
page.

Regards, Dave S

----- Original Message -----
From: "partha_6_6"; < partha_6_6%40yahoo.com">partha_6_6yahoo.com>
To: < JavaScript_Official%40yahoogroups.com">JavaScript_Officialyahoogroups.com>
Sent: Saturday, June 16, 2007 6:08 AM
Subject: [JavaScript] Why it focus is not working with this syntex

> Why this code is not working.
> When i have clicked the 'click' button , i have shown a alert box and
> after clicking that i want to focus the text field.
&gt;
> it is not working with the following code.
&gt;
> main.js--------------
>
> function doFocus(myForm,myField)
> {
> alert('abcd');
>; //document.getElementById(myField).focus();
> document.myForm.myField.focus();
> }
>
> index.php---------------
>;
> <html&gt;
> <head&gt;
> <SCRIPT language=JavaScript src="Main.js&quot;></SCRIPT>
> </head&gt;
> <body&gt;
> <form name=";field"; id="field"&gt;
> <table width=&quot;800" border=&quot;1" height=&quot;500"; align=&quot;center&quot;>
> <tr>
> <td><INPUT type=";text" name=";pass" id="pass"&gt;</td&gt;
> <tr>
> <td><INPUT type=";button&quot; name=";bt1" id="bt1" onclick=&quot;doFocus
> ('field','pass');&quot; width=&quot;10" value=&quot;click&quot;></td>
&gt; </tr>;
> </tr>;
> </table>
> </form&gt;
> </body&gt;
> </html&gt;
>
>
&gt; This code is not working with the syntex
&gt;
> document.myForm.myField.focus(); in main.js file.
&gt;
> but if i place this systex
&gt;
> document.getElementById(myField).focus();
>
&gt; then it is working,
>
>;
> help me.
>
>
&gt;
>
> Visit http://aiaiai.com for more groups to join
>; Yahoo! Groups Links
&gt;
>
>

__._,_.___
.

__,_._,___
Re: Why it focus is not working with this syntex
country flaguser name
United States
2007-06-15 19:32:01

Hi,

document.getElementById("element name as string&quot;) accepts a string
argument that you are passing correctly through myField.

document.myForm.myField.focus() requires that myForm refer to the
name of a form object, not a string "myForm". The same applies to
myField, it must be an object reference to an element and not a
string "myField". As the function is written now it is as if you were
stating:

document."field"."pass".focus()

and that is a syntax error. It should be:

document.field.pass.focus()

If you want to utilize document.field.pass.focus() do not equip the
function with parameters. Or if you must use string arguments with
your function, use:

document.forms[myForm].elements[myField].focus();

in the function body and keep the function call like you have it.
Javascript allows a form and its elements to be accessed array-style.
The array index can be a string or an integer.

Paul

--- In JavaScript_Official%40yahoogroups.com">JavaScript_Officialyahoogroups.com, "partha_6_6";
<partha_6_6...> wrote:
&gt;
> Why this code is not working.
> When i have clicked the 'click' button , i have shown a alert box
and
> after clicking that i want to focus the text field.
&gt;
> it is not working with the following code.
&gt;
> main.js--------------
>
> function doFocus(myForm,myField)
> {
> alert('abcd');
>; //document.getElementById(myField).focus();
> document.myForm.myField.focus();
> }
>
> index.php---------------
>;
> <html&gt;
> <head&gt;
> <SCRIPT language=JavaScript src="Main.js&quot;></SCRIPT>
> </head&gt;
> <body&gt;
> <form name=";field"; id="field"&gt;
> <table width=&quot;800" border=&quot;1" height=&quot;500"; align=&quot;center&quot;>
> <tr>
> <td><INPUT type=";text" name=";pass" id="pass"&gt;</td&gt;
> <tr>
> <td><INPUT type=";button&quot; name=";bt1" id="bt1" onclick=&quot;doFocus
> ('field','pass');&quot; width=&quot;10" value=&quot;click&quot;></td>
&gt; </tr>;
> </tr>;
> </table>
> </form&gt;
> </body&gt;
> </html&gt;
>
>
> This code is not working with the syntex
&gt;
> document.myForm.myField.focus(); in main.js file.
&gt;
> but if i place this systex
>
> document.getElementById(myField).focus();
>
> then it is working,
>
>
> help me.
>

__._,_.___
.

__,_._,___
Re: Why it focus is not working with this syntex
user name
2007-06-16 09:52:37

because you are trying to set the focus to myForm.myField which does not exist.
Your form is called "field" and your textfield is called "pass".
So replace the focus line with:
document.field.pass.focus()

or

document.getElementById("pass&quot;).focus(); --- You might want to check that getElementById does not return a falsie value before setting the focus with this approach.

Alligator_666

partha_6_6 < partha_6_6%40yahoo.com">partha_6_6yahoo.com> wrote: Why this code is not working.
When i have clicked the 'click' button , i have shown a alert box and
after clicking that i want to focus the text field.

it is not working with the following code.

main.js--------------

function doFocus(myForm,myField)
{
alert('abcd');
//document.getElementById(myField).focus();
document.myForm.myField.focus();
}

index.php---------------

<html&gt;
<head&gt;
<SCRIPT language=JavaScript src="Main.js&quot;></SCRIPT>
</head&gt;
<body&gt;
<form name=";field"; id="field"&gt;
<table width=&quot;800" border=&quot;1" height=&quot;500"; align=&quot;center&quot;>
<tr>
<td><INPUT type=";text" name=";pass" id="pass"&gt;</td&gt;
<tr>
<td><INPUT type=";button&quot; name=";bt1" id="bt1" onclick=&quot;doFocus
('field','pass');&quot; width=&quot;10" value=&quot;click&quot;></td>
</tr>;
</tr>;
</table>
</form&gt;
</body&gt;
</html&gt;

This code is not working with the syntex

document.myForm.myField.focus(); in main.js file.

but if i place this systex

document.getElementById(myField).focus();

then it is working,

help me.





---------------------------------
Be a better Globetrotter. Get better travel answers from someone who knows.
Yahoo! Answers - Check it out.

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )