OnBlur() focus problems

R

Robert Oschler

I have a textarea element that I have created an onblur() handler for.

In the onblur() handler, I check to make sure that they have saved the
contents of the edit box, before leaving it.

If they have not saved the contents, when they exit the edit box, I put up
an alert telling them to perform the save.

My handler (note: the editBox variable was properly assigned a reference to
the desired HTML textarea element earlier):

function onEditBoxBlur()
{
if (bNotSaved)
{
alert("Please save your new text.");
editBox.focus();
return false;
}
return true;
}

However, after they hit OK to the alert box, focus ends up in a textarea
element *below* the desired edit box (the one has the onEditBoxBlur() as
it's "onblur" handler).

If I remove the editBox.focus() statement, then focus returns to the web
page itself instead.

Why is this happening and how can I fix things so that focus returns to the
desired textarea control ("editBox")?

Thanks.
 
R

Robert Oschler

Xandax said:
Are you sure you have the correct element when setting the focus?

Xandax,

Yes. I know this because I use the same variable that contains the element
node reference, to set the focus to that element when the page first starts
up.

Thanks.
 
R

Randy Webb

Robert Oschler said the following on 8/25/2005 3:41 AM:
I have a textarea element that I have created an onblur() handler for.

onblur is a bad idea.
In the onblur() handler, I check to make sure that they have saved the
contents of the edit box, before leaving it.

How do they save the contents without blurring the text box? If they
have to leave the box to save it, then your onblur gets fired, but thats
a drawback of the onblur. Consider onChange instead.
If they have not saved the contents, when they exit the edit box, I put up
an alert telling them to perform the save.

My handler (note: the editBox variable was properly assigned a reference to
the desired HTML textarea element earlier):

How is it defined and assigned though? That may very well be the
problem, but without seeing the code that assigns it.....
function onEditBoxBlur()
{
if (bNotSaved)
{
alert("Please save your new text.");
editBox.focus();
return false;
}
return true;
}

However, after they hit OK to the alert box, focus ends up in a textarea
element *below* the desired edit box (the one has the onEditBoxBlur() as
it's "onblur" handler).

That indicates that your editBox variable is not assigned to the correct
textArea.

Or, that bNotSaved is not defined properly.
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top