Focus on element not working correctly

Focus on element not working correctly

am 04.10.2007 01:43:08 von Adrienne Boswell

Here is the scenario. I have an alert that fires if the user has not
filled in a field. What I would like to do is have the browser focus
on the message (div id="message"). That div is generated only if
there is an error, and the server side code is placed within the
head. What I think is happening is the js focus is firing BEFORE the
div is rendered, because I am getting the js error
document.getElementById("") is null or is not an object. This is not
something vital, just something I think would be nice for the user
(especially if the form and message are below the fold). I have
included code and markup below. (no URL - this is stripped down to
what's necessary).

<% option explicit%>

<%
dim fname, isget

if request.servervariables("REQUEST_METHOD") = "POST" then
ispost = true
elseif request.servervariables("REQUEST_METHOD") = "GET" then
isget = true
end if

if ispost then %>
if request.form("fname") = "" then
required = "fname"
message = "First name is required"
end if

if message <> "" then
%>

<% message = "

">" & message & "
"

end if

fname = "Your first name"

end if%>



%>">
<%=message%>
* indicates Required
fields


isget then%>onfocus="if(this.value == '<%=fname%>') this.value = '';"<
%end if %>>

name="lname" id="lname" value="<%=lname%>" <%if isget then
%>onfocus="if(this.value == '<%=lname%>') this.value = '';"<%end if
%>>





Re: Focus on element not working correctly

am 04.10.2007 01:44:42 von Adrienne Boswell

On Oct 3, 4:43 pm, Adrienne Boswell wrote:
> Here is the scenario. I have an alert that fires if the user has not
> filled in a field. What I would like to do is have the browser focus
> on the message (div id="message"). That div is generated only if
> there is an error, and the server side code is placed within the
> head. What I think is happening is the js focus is firing BEFORE the
> div is rendered, because I am getting the js error
> document.getElementById("") is null or is not an object. This is not
> something vital, just something I think would be nice for the user
> (especially if the form and message are below the fold). I have
> included code and markup below. (no URL - this is stripped down to
> what's necessary).
>
> <% option explicit%>
>
> <%
> dim fname, isget

forgot to dim lname as well, would have in real life


>
> if request.servervariables("REQUEST_METHOD") = "POST" then
> ispost = true
> elseif request.servervariables("REQUEST_METHOD") = "GET" then
> isget = true
> end if
>
> if ispost then %>
> if request.form("fname") = "" then
> required = "fname"
> message = "First name is required"
> end if
>
> if message <> "" then
> %>
>
> <% message = "

> ">" & message & "
"
>
> end if
>
> fname = "Your first name"
>
> end if%>
>
>
>
>
> %>">
> <%=message%>
>
* indicates Required
> fields

>
> > isget then%>onfocus="if(this.value == '<%=fname%>') this.value = '';"<
> %end if %>>

> > name="lname" id="lname" value="<%=lname%>" <%if isget then
> %>onfocus="if(this.value == '<%=lname%>') this.value = '';"<%end if
> %>>

>

>


>

>
>

Re: Focus on element not working correctly

am 04.10.2007 01:46:18 von Adrienne Boswell

On Oct 3, 4:43 pm, Adrienne Boswell wrote:
> Here is the scenario. I have an alert that fires if the user has not
> filled in a field. What I would like to do is have the browser focus
> on the message (div id="message"). That div is generated only if
> there is an error, and the server side code is placed within the
> head. What I think is happening is the js focus is firing BEFORE the
> div is rendered, because I am getting the js error
> document.getElementById("") is null or is not an object. This is not
> something vital, just something I think would be nice for the user
> (especially if the form and message are below the fold). I have
> included code and markup below. (no URL - this is stripped down to
> what's necessary).
>
> <% option explicit%>
>

(docheader_inc.asp has a lot of frequently used variables already
dimmed (like required))

> <%
> dim fname, isget
>
> if request.servervariables("REQUEST_METHOD") = "POST" then
> ispost = true
> elseif request.servervariables("REQUEST_METHOD") = "GET" then
> isget = true
> end if
>
> if ispost then %>
> if request.form("fname") = "" then
> required = "fname"
> message = "First name is required"
> end if
>
> if message <> "" then
> %>
>
> <% message = "

> ">" & message & "
"
>
> end if
>
> fname = "Your first name"
>
> end if%>
>
>
>
>
> %>">
> <%=message%>
>
* indicates Required
> fields

>
> > isget then%>onfocus="if(this.value == '<%=fname%>') this.value = '';"<
> %end if %>>

> > name="lname" id="lname" value="<%=lname%>" <%if isget then
> %>onfocus="if(this.value == '<%=lname%>') this.value = '';"<%end if
> %>>

>

>


>

>
>

Re: Focus on element not working correctly

am 04.10.2007 03:11:55 von David Mark

On Oct 3, 7:43 pm, Adrienne Boswell wrote:
> Here is the scenario. I have an alert that fires if the user has not
> filled in a field. What I would like to do
is have the browser focus

Bad idea. Validate on submission. At the very least, leave the focus
alone during input.

> on the message (div id="message"). That div is generated only if

Why do you want to focus the div? It won't work at all in some
browsers and you don't have a tabIndex on it anyway.

> there is an error, and the server side code is placed within the
> head. What I think is happening is the js focus is firing BEFORE the
> div is rendered, because I am getting the js error
> document.getElementById("") is null or is not an object. This is not

document.getElementById("") is obviously wrong.

> something vital, just something I think would be nice for the user
> (especially if the form and message are below the fold). I have
> included code and markup below. (no URL - this is stripped down to
> what's necessary).
>
> <% option explicit%>
>

Don't post server side code. Post the output of the ASP.

Re: Focus on element not working correctly

am 04.10.2007 03:49:39 von RobG

On Oct 4, 9:43 am, Adrienne Boswell wrote:
> Here is the scenario. I have an alert that fires if the user has not
> filled in a field. What I would like to do is have the browser focus
> on the message (div id="message").

Div element's don't necessarly support the focus method, calling it
may well result in an error in some browsers. In those that do
support it, what do you expect to happen? It does nothing useful in
either Firefox or IE as far as I can tell.


> That div is generated only if
> there is an error, and the server side code is placed within the
> head.

As far as I can tell, the html for the div is generated by the server
and placed in the body, but the focus code is in the head of your HTML
page as global code - it will be called as soon as it is loaded. That
means that when the code is run, the div can't exist yet.


> What I think is happening is the js focus is firing BEFORE the
> div is rendered, because I am getting the js error
> document.getElementById("") is null or is not an object. This is not

That seems a reasonable conclusion.


> something vital, just something I think would be nice for the user
> (especially if the form and message are below the fold). I have
> included code and markup below. (no URL - this is stripped down to
> what's necessary).

If your validation is creating the div, then you already have a
reference to it, you don't need getElementById.

>
> <% option explicit%>
>

This is a javascript news group, don't expect anyone to be able to
debug asp code. Post what is actually received by the page, how you
get it there is not relevant here.


[...]
>

Which executes as soon as that script element loads -- before anything
in the body (including your div) has loaded. Try

>

Re: Focus on element not working correctly

am 04.10.2007 08:20:40 von Adrienne Boswell

Gazing into my crystal ball I observed Noah Sussman
writing in news:1191464745.415718.164210@50g2000hsm.googlegroups.com:

> On Oct 3, 7:43 pm, Adrienne Boswell wrote:
>> What I think is happening is the js focus is firing BEFORE the
>> div is rendered, because I am getting the js error
>> document.getElementById("") is null or is not an object.
>
> You are correct. In your document's head, you have:
>
>>
>
> Which executes as soon as that script element loads -- before anything
> in the body (including your div) has loaded. Try
>
>>
>
>
>

Thank you so much, will give it go in the morning.

--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share

Re: Focus on element not working correctly

am 04.10.2007 08:26:06 von Adrienne Boswell

Gazing into my crystal ball I observed RobG writing
in news:1191462579.164831.15460@o80g2000hse.googlegroups.com:

> On Oct 4, 9:43 am, Adrienne Boswell wrote:
>> Here is the scenario. I have an alert that fires if the user has not
>> filled in a field. What I would like to do is have the browser focus
>> on the message (div id="message").
>
> Div element's don't necessarly support the focus method, calling it
> may well result in an error in some browsers. In those that do
> support it, what do you expect to happen? It does nothing useful in
> either Firefox or IE as far as I can tell.

It moves the page to the specified DIV, similar to a page fragment, href="#message">, etc.

>
>
>> That div is generated only if
>> there is an error, and the server side code is placed within the
>> head.
>
> As far as I can tell, the html for the div is generated by the server
> and placed in the body, but the focus code is in the head of your HTML
> page as global code - it will be called as soon as it is loaded. That
> means that when the code is run, the div can't exist yet.
>
>
>> What I think is happening is the js focus is firing BEFORE the
>> div is rendered, because I am getting the js error
>> document.getElementById("") is null or is not an object. This is not
>
> That seems a reasonable conclusion.
>
>
>> something vital, just something I think would be nice for the user
>> (especially if the form and message are below the fold). I have
>> included code and markup below. (no URL - this is stripped down to
>> what's necessary).
>
> If your validation is creating the div, then you already have a
> reference to it, you don't need getElementById.
>
>>
>> <% option explicit%>
>>
>
> This is a javascript news group, don't expect anyone to be able to
> debug asp code. Post what is actually received by the page, how you
> get it there is not relevant here.

Actually, it's a cross post to client and server side groups.

>
>
> [...]
>>
> <% message = "

"
>
> end if
>
> fname = "Your first name"
>
> end if%>
>
>
>
>
> %>">
> <%=message%>
>
* indicates Required
> fields

>
> > isget then%>onfocus="if(this.value == '<%=fname%>') this.value = '';"<
> %end if %>>

> > name="lname" id="lname" value="<%=lname%>" <%if isget then
> %>onfocus="if(this.value == '<%=lname%>') this.value = '';"<%end if
> %>>

>

>


>

>
>
>

Re: Focus on element not working correctly

am 04.10.2007 13:00:07 von reb01501

David Mark wrote:
> On Oct 3, 7:43 pm, Adrienne Boswell wrote:
>> Here is the scenario. I have an alert that fires if the user has not
>> filled in a field. What I would like to do
> is have the browser focus
>
> Bad idea. Validate on submission. At the very least, leave the focus
> alone during input.
>
Why not do both? Just because she's attempting to do it client-side does not
mean she is failing to do it server-side.

> Don't post server side code. Post the output of the ASP.

Yes, the initial focus should be writing the html that produces the desired
outcome. Then write the asp code that produces that html.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Focus on element not working correctly

am 04.10.2007 13:02:07 von reb01501

RobG wrote:
>
> This is a javascript news group,

Huh? look again :-)

The rest of your points are certainly valid though.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Focus on element not working correctly

am 04.10.2007 13:03:38 von reb01501

RobG wrote:
> This is a javascript news group,
Oh wait - the crosspost got lost somewhere, probably due to crossposting
between different news servers

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: Focus on element not working correctly

am 04.10.2007 15:39:43 von dorward

On Oct 4, 12:43 am, Adrienne Boswell wrote:
> Here is the scenario. I have an alert that fires if the user has not
> filled in a field. What I would like to do is have the browser focus
> on the message (div id="message").

A div can't hold the focus, it isn't something the user can interact
with.

> That div is generated only if
> there is an error, and the server side code is placed within the
> head. What I think is happening is the js focus is firing BEFORE the
> div is rendered, because I am getting the js error
> document.getElementById("") is null or is not an object.

Well yes. The
> <% message = "

> ">" & message & "
"
>
> end if
>
> fname = "Your first name"
>
> end if%>
>
>
>
>
> %>">
> <%=message%>
>
* indicates Required
> fields

>
> > isget then%>onfocus="if(this.value == '<%=fname%>') this.value = '';"<
> %end if %>>

> > name="lname" id="lname" value="<%=lname%>" <%if isget then
> %>onfocus="if(this.value == '<%=lname%>') this.value = '';"<%end if
> %>>

>

>


>

>
>
>

document.getElementById... must always be used in not
(basically).

--
Neredbojias
Half lies are worth twice as much as whole lies.

Re: Focus on element not working correctly

am 05.10.2007 01:40:58 von Vaxius

On Oct 3, 6:43 pm, Adrienne Boswell wrote:
> Here is the scenario. I have an alert that fires if the user has not
> filled in a field. What I would like to do is have the browser focus
> on the message (div id="message"). That div is generated only if
> there is an error, and the server side code is placed within the
> head. What I think is happening is the js focus is firing BEFORE the
> div is rendered, because I am getting the js error
> document.getElementById("") is null or is not an object. This is not
> something vital, just something I think would be nice for the user
> (especially if the form and message are below the fold). I have
> included code and markup below. (no URL - this is stripped down to
> what's necessary).
>
> <% option explicit%>
>
> <%
> dim fname, isget
>
> if request.servervariables("REQUEST_METHOD") = "POST" then
> ispost = true
> elseif request.servervariables("REQUEST_METHOD") = "GET" then
> isget = true
> end if
>
> if ispost then %>
> if request.form("fname") = "" then
> required = "fname"
> message = "First name is required"
> end if
>
> if message <> "" then
> %>
>
> <% message = "

> ">" & message & "
"
>
> end if
>
> fname = "Your first name"
>
> end if%>
>
>
>
>
> %>">
> <%=message%>
>
* indicates Required
> fields

>
> > isget then%>onfocus="if(this.value == '<%=fname%>') this.value = '';"<
> %end if %>>

> > name="lname" id="lname" value="<%=lname%>" <%if isget then
> %>onfocus="if(this.value == '<%=lname%>') this.value = '';"<%end if
> %>>

>

>


>

>
>

If I understand right, what you want to do is focus on the input field
the user left blank. There's no need to try to draw attention to the
message, because it's already displayed in a pop-up alert box.