|
List Info
Thread: Why it focus is not working with this syntex
|
|
| Why it focus is not working with this
syntex |
  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"></SCRIPT>
</head>
<body>
<form name="field" id="field">
<table width="800" border="1" height="500" align="center">
<tr>
<td><INPUT type="text" name="pass" id="pass"></td>
<tr>
<td><INPUT type="button" name="bt1" id="bt1" onclick="doFocus
('field','pass');" width="10" value="click"></td>
</tr>
</tr>
</table>
</form>
</body>
</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 |
  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_6 yahoo.com>
To: < JavaScript_Official%40yahoogroups.com">JavaScript_Official yahoogroups.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.
>
> 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"></SCRIPT>
> </head>
> <body>
> <form name="field" id="field">
> <table width="800" border="1" height="500" align="center">
> <tr>
> <td><INPUT type="text" name="pass" id="pass"></td>
> <tr>
> <td><INPUT type="button" name="bt1" id="bt1" onclick="doFocus
> ('field','pass');" width="10" value="click"></td>
> </tr>
> </tr>
> </table>
> </form>
> </body>
> </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.
>
>
>
>
> Visit http://aiaiai.com for more groups to join
> Yahoo! Groups Links
>
>
>
__._,_.___
.
__,_._,___
|
| Re: Why it focus is not working with
this syntex |
  United States |
2007-06-15 19:32:01 |
|
Hi,
document.getElementById("element name as string") 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_Official yahoogroups.com, "partha_6_6"
<partha_6_6 ...> 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>
> <head>
> <SCRIPT language=JavaScript src="Main.js"></SCRIPT>
> </head>
> <body>
> <form name="field" id="field">
> <table width="800" border="1" height="500" align="center">
> <tr>
> <td><INPUT type="text" name="pass" id="pass"></td>
> <tr>
> <td><INPUT type="button" name="bt1" id="bt1" onclick="doFocus
> ('field','pass');" width="10" value="click"></td>
> </tr>
> </tr>
> </table>
> </form>
> </body>
> </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 |

|
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").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_6 yahoo.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>
<head>
<SCRIPT language=JavaScript src="Main.js"></SCRIPT>
</head>
<body>
<form name="field" id="field">
<table width="800" border="1" height="500" align="center">
<tr>
<td><INPUT type="text" name="pass" id="pass"></td>
<tr>
<td><INPUT type="button" name="bt1" id="bt1" onclick="doFocus
('field','pass');" width="10" value="click"></td>
</tr>
</tr>
</table>
</form>
</body>
</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.
---------------------------------
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 )
|