...

View Full Version : I'm getting error while focusing.



Srikanth.S
05-25-2005, 02:48 PM
Hi Everyone,
I'm a new member to this forum and my question as follows.I'm getting a error in my javascript code while i used to do a delete operation.The code i used in my onload event(inside the body tag) is document.changepassword_form.username.focus() and i'm getting the error as
document.changepassword_form.username is null or not an object.Any idea abt this pls reply.

SpirtOfGrandeur
05-25-2005, 03:20 PM
That means that the JS cannot find the object. Which either means the objects name has changed. Or it is not loaded on before the event occurs!

rlemon
05-25-2005, 03:22 PM
try

var myElement = document.getElementById('elementIDValue');
var myElement.focus();

where elementIDValue = the id value for the form element you wish to set focus to.

Srikanth.S
05-25-2005, 03:26 PM
That means that the JS cannot find the object. Which either means the objects name has changed. Or it is not loaded on before the event occurs!

Hi,
Thanks for ur kind reply.I understood something from ur reply, but give me a further solution to solve this problem.Bcos I'm a beginner to JS so provide me with a code solution for this.

rlemon
05-25-2005, 04:15 PM
well, asking for a code solution is probably not the best way to go about getting it.

maybe if you post a solution you have attempted we can help (the html with the corresponding JS code).

however, here is an example of what you want.


JavaScript


function setFocus(id){
var myElement = document.getElementById(id)
myElement.focus();
}


HTML


<body onload="setFocus('username')">
<form>
<input type="text" name="username" id="username" maxlength="128">
.......
.......
.......
</form>


where
.......
.......
.......
represents the rest of your form code.
[/code]

Srikanth.S
05-26-2005, 05:46 AM
well, asking for a code solution is probably not the best way to go about getting it.

maybe if you post a solution you have attempted we can help (the html with the corresponding JS code).

however, here is an example of what you want.


JavaScript


function setFocus(id){
var myElement = document.getElementById(id)
myElement.focus();
}


HTML


<body onload="setFocus('username')">
<form>
<input type="text" name="username" id="username" maxlength="128">
.......
.......
.......
</form>


where
.......
.......
.......
represents the rest of your form code.
[/code]


Hi,
Thanks for ur kind reply.Well u asked abt my previous trials to solve my issue, they are as follows

1.First I tried like this inside the head tag.

onload=function()
{
document.changepassword_form.username.focus()
}
2.My second trial was next to my </form> tag.

<script type="text/javascript">
document.changepassword_form.username.focus();
</script>
I finally tried ur solution unfortunately it also produces the same result.
Any idea abt this pls reply.

glenngv
05-26-2005, 06:28 AM
Is your form named changepassword_form and username field named username? You may need to post the HTML too.

Srikanth.S
05-26-2005, 07:10 AM
Hi,
Here is my HTML code for ur reference.

<form name="changepassword_form" method="post" action="useraccountAPI.sgp" onSubmit="Javascript: return confirmDelete();">
---------------------------------
---------------------------------
<input type="text" class="sgTextBox" name="username" id="username" style="width:150; height:20;" autocomplete="off" maxlength="16"/ tabindex="1">
--------------------------
--------------------------
</form>

My onload event is in another file.Is it enough or u need some more pls reply.

glenngv
05-26-2005, 07:26 AM
Maybe the path to the external js file is wrong?

_Aerospace_Eng_
05-26-2005, 07:41 AM
I was having a problem similar earlier today. I discovered if the element wasn't created yet, then the script wouldn't run. A question I had was when does an onload in the body tag run? After the page loads completely or sometime during the page load?

With that said try this:

<script type="text/javascript">
<!--
function focusbox(){
document.changepassword_form.username.focus();
}
//-->
</script>
Put that in your head tags.
In your body tag add onload="focusbox()"

<body onload="focusbox()">
So it looks like this which does work.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function focusbox(){
document.changepassword_form.username.focus();
}
//-->
</script>
</head>

<body onload="focusbox()">
<form name="changepassword_form" method="post" action="useraccountAPI.sgp" onSubmit="javascript: return confirmDelete();">
---------------------------------
---------------------------------
<input type="text" class="sgTextBox" name="username" id="username" style="width:150; height:20;" autocomplete="off" maxlength="16"/ tabindex="1">
--------------------------
--------------------------
</form>
</body>
You might want to add units to your width and height in your inline styles

rlemon
05-26-2005, 03:53 PM
why is there a random backslash in your input element.?

remove that and try my code. I tested my code on my local pc and it works fine. (it's not hard to code).

glenngv
05-27-2005, 03:35 AM
why is there a random backslash in your input element.?

remove that and try my code. I tested my code on my local pc and it works fine. (it's not hard to code).
The floating slash inside the tag will be ignored by the browser and doesn't affect anything. But of course, it should be removed.

As I said earlier, the path to the external js might be wrong. Or probably the js file contains <script></script> tags or HTML comments <!-- -->



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum