473,657 Members | 2,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

focus() won't work when function called with onblur

Tearing hair out time! Simple attached page shows the problem.
http://www.brilley.co.uk/TestFocusSelect.htm

Using a function to test if too many characters have been keyed in to a
textarea. Obviously, must return focus and select to the textarea if too
many have been keyed.

When the function is called with onblur, it works in IE but not in Safari or
NN. The function is called but the focus() and select() methods fail.

Something stupid I'm doing, I'll be bound.

Thanking you in anticipation.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #1
9 3698
Roger Withnell wrote:
[snip]
When the function is called with onblur, it works in IE but not in Safari or
NN. The function is called but the focus() and select() methods fail.

Something stupid I'm doing, I'll be bound.

[snip]

First, I *hate* things that trap me - with your form, the
user can't escape until they meet your criterion for the
textarea and enter zero to three characters. The event will
keep firing every time the box goes in and out of focus, so
if I change to another application, your alert keeps poping
up when I return to the browser.

So, please think carefully if there is some other (kinder)
way to do what you want.

The following is in regard to Firefox, but I suspect the
same is true for NN and Safari...

If you use an external button to run your script, it works
as expected - the alert pops up and when dismissed, the text
in the text area is selected. Incidentally, there is no
need to call focus(), select() will set focus on the
selected text (same for IE 6).

However, when run using onblur, closing the alert does not
seem to return focus to the textarea. When I subsequently
click on the textarea, it is selected for a moment, then a
cursor is placed in the text roughly where I clicked.

This indicates that the textarea content is not selected
until I put focus back on the textarea - but now it must
deal with the click, so it un-selects the text and puts
focus where the click occured. Perhaps execution of the
select is stopped until focus is returned to the textbox?

If I change to another application, then return to Firefox,
the text is selected. So it seems the "onblur" does not
really finish until the document or window loses focus.
Also, if I put focus on the textarea then change
applications, the onblur fires twice so I have to deal with
two alerts.

I tried a few work arounds, they were all messy and very
unsatisfactory. I'd just avoid this altogether and validate
the textarea on some other event.

Rob.
Jul 23 '05 #2
Rob

Thanks for your thorough and extensive analysis.

My problem is that the form is putting the text into a database table field
which has a finite size. So I must stop the user entering more characters
that the database table wants. I don't want to discard the text that is
over the limit. I want to give the user the choice to adjust his text
within the limit set by the database table.

How do you suggest I achieve this?

"RobG" <rg***@iinet.ne t.auau> wrote in message
news:GG******** *********@news. optus.net.au...
Roger Withnell wrote:
[snip]
When the function is called with onblur, it works in IE but not in Safari
or NN. The function is called but the focus() and select() methods fail.

Something stupid I'm doing, I'll be bound.

[snip]

First, I *hate* things that trap me - with your form, the user can't
escape until they meet your criterion for the textarea and enter zero to
three characters. The event will keep firing every time the box goes in
and out of focus, so if I change to another application, your alert keeps
poping up when I return to the browser.

So, please think carefully if there is some other (kinder) way to do what
you want.

The following is in regard to Firefox, but I suspect the same is true for
NN and Safari...

If you use an external button to run your script, it works as expected -
the alert pops up and when dismissed, the text in the text area is
selected. Incidentally, there is no need to call focus(), select() will
set focus on the selected text (same for IE 6).

However, when run using onblur, closing the alert does not seem to return
focus to the textarea. When I subsequently click on the textarea, it is
selected for a moment, then a cursor is placed in the text roughly where I
clicked.

This indicates that the textarea content is not selected until I put focus
back on the textarea - but now it must deal with the click, so it
un-selects the text and puts focus where the click occured. Perhaps
execution of the select is stopped until focus is returned to the textbox?

If I change to another application, then return to Firefox, the text is
selected. So it seems the "onblur" does not really finish until the
document or window loses focus. Also, if I put focus on the textarea then
change applications, the onblur fires twice so I have to deal with two
alerts.

I tried a few work arounds, they were all messy and very unsatisfactory.
I'd just avoid this altogether and validate the textarea on some other
event.

Rob.


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #3
Roger Withnell wrote:
Rob

Thanks for your thorough and extensive analysis.

My problem is that the form is putting the text into a database table field
which has a finite size. So I must stop the user entering more characters
that the database table wants. I don't want to discard the text that is
over the limit. I want to give the user the choice to adjust his text
within the limit set by the database table.

How do you suggest I achieve this?


The typical method is to validate the form when the user
submits it, firstly at the client then at the server. You
must do server validation as you have no way of knowing if
your validation has actually been run on the string
submitted to the server (user may have JS turned off, may be
spoofing your server by creating their own submit, modifying
the URI parameters manually, etc.)

So a good way is to put an alert right next to the text
field that shows up if they don't do it right. Hide the
message using an onload function, that way if JS is turned
off the message shows all the time. If the user gets it
wrong, show the message. When they get it right, hide it
again. This way, you don't care how often the onblur fires
since the action it takes is quite innocuous.

Another way would be to show the error text in a soft colour
(say light blue or pink), then if they get it wrong, change
the style to bold red.

I haven't played with the layout, but immediately to the
right is usually best.

Minimial implementation follows:

<html>
<head>
<title>Entry Error Play</title>
<script type="text/javascript">
function boxCheck(a) {
var aErr = document.getEle mentById('aTAer ror');
if (a.value.length > '3') {
alert('Ooops');
aErr.style.visi bility = 'visible';
} else {
aErr.style.visi bility = 'hidden';
}
}

function hideError() {
document.getEle mentById('aTAer ror').style.vis ibility =
'hidden';
}
</script>

</head>

<body onload="hideErr or();">

<form name="me2" action="">
<textarea name="aTA" value="start_va lue"
onblur="boxChec k(this.form.aTA );
"></textarea>
<div id="aTAerror"
style="color: red;
font-weight: bold;
font-family: arial, sans-serif;">
You must enter a maximum of three characters</div>
</form>
</body>
</html>

Jul 23 '05 #4
RobG wrote:

[snip]
alert('Ooops');

[snip]

Ooops!! Get rid of the rotten alert, it was there for debug
only - it ensures IE traps you in the text area.

The checkBox function should have been:

function boxCheck(a) {
var aErr = document.getEle mentById('aTAer ror');
if (a.value.length > '3') {
aErr.style.visi bility = 'visible';
} else {
aErr.style.visi bility = 'hidden';
}
}
Rob.
Jul 23 '05 #5
Roger Withnell wrote:
Rob

Thanks for your thorough and extensive analysis.

My problem is that the form is putting the text into a database table field
which has a finite size. So I must stop the user entering more characters
that the database table wants. I don't want to discard the text that is
over the limit. I want to give the user the choice to adjust his text
within the limit set by the database table.

How do you suggest I achieve this?


Part deux:

I also tried using <label> as suggested by Mike Winter in a
previous thread(1), however it doesn't seem to have a style
property that can be manipulated. The OP of that thread
settled for using <span> rather than <div> when adding a
caption to a textarea.

Your choice - Rob.

1.
Mike wrote:

As the text probably qualifies as a label, the LABEL
element would be the best to use:

<label for="myInput">< input id="myInput" type="checkbox" ...>
Your text here</label>

All DOM-supporting browsers should access that label text with:

inputObj.nextSi bling.data

(Verified with the latest versions of Opera, IE, Mozilla,
Netscape, and Firefox)

When you change the value, remember to add a space before
the text so that it doesn't appear flush next to the
checkbox.
Jul 23 '05 #6
On Fri, 08 Oct 2004 03:55:48 GMT, RobG <rg***@iinet.ne t.auau> wrote:

[snip]
I also tried using <label> as suggested by Mike Winter in a
previous thread(1), however it doesn't seem to have a style
property that can be manipulated.
What are you trying to manipulate, the LABEL element or its contents?
LABELs certainly can be styled, unless you're using a less-than-dynamic
browser (which shouldn't include the "major" modern ones).
The OP of that thread settled for using <span> rather than
<div> when adding a caption to a textarea.


Yes. The OP never said why.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #7
Michael Winter wrote:
[snip]
What are you trying to manipulate, the LABEL element or its contents?
LABELs certainly can be styled, unless you're using a less-than-dynamic
browser (which shouldn't include the "major" modern ones).


I tried playing with the label.style in Firefox, but it just kept
returning "undefined" . I had no problem applying an in-line style but
there doesn't seem to be an interface to access it.

I wanted to either toggle it's visibility or colour but couldn't seem to
access it the same way you can manipulate other elements.

Rob.
Jul 23 '05 #8
On Sat, 09 Oct 2004 00:41:49 +1000, RobG <rg***@iinet.ne t.auau> wrote:
I tried playing with the label.style in Firefox, but it just kept
returning "undefined" . I had no problem applying an in-line style but
there doesn't seem to be an interface to access it.


<label for="myInput">M y label:
<input id="myInput"
onfocus="this.p arentNode.style .color = '#0000ff';"
onblur="this.pa rentNode.style. color = '';">
</label>

Firefox 0.9.3 has no problem with the above. The text, "My label:", does
turn blue as a result of giving focus to the label, whether by clicking on
the component or on the label itself.

Are you sure you aren't making some silly mistake resulting in you trying
to get the style object on a non-element node?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9
Michael Winter wrote:
[snip]
Are you sure you aren't making some silly mistake resulting in you
trying to get the style object on a non-element node?

Mike


Very likely. I was using nextSibling.sty le, as nextSibling.dat a
returns the text. It never occured to me to use parentNode, though
clearly the structure of the HTML indicates that would have been
correct (20/20 hindsight, eh?).

I guess nextSibling is actually getting the text element of the label,
whereas I should have been playing with the style of the label itself.

I'll have another play in the morning ... kinda late now.

Cheers, Rob.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14281
by: fish | last post by:
Hi, I have an HTML page with a FORM and some input fields. On the fields I wish to do validation as the punters change the field values. If they get it wrong, then I tell them and then wish to put the focus back to the offending field. ( It works if a use an 'onblur' event but not an 'onchange' )
1
4057
by: ra | last post by:
Hi I can't figure this out - I have a routine that is triggered after a text field is changed and if an error it displays an alert message and should put focus back to the field in error but focus or select won't seem to work - the cursor just stays in the next field. Im using IE6.0 The code is like
10
6709
by: CES | last post by:
All, I'm having a problem returning focus back to an input field in Netscape. The code works in IE and Opera but not in netscape6+. Basically I have a function that is called upon exiting a form field, if the
5
11368
by: tshad | last post by:
I have a date validation function that I want to stay at the object I am validating if there is a Validation error, but it always goes to the next object. The Javascript: function ValidateForm(me){ var dt=me if (isDate(dt.value)==false){ dt.focus()
9
2404
by: fidodido | last post by:
If in the textarea (textarea3), the value is not "abc", and the user uses "Tab" to go to the next textarea (textarea4), it will alert an error message...and the focus will return to the textarea (textarea3) again... It works in Internet Explorer, however in firefox it does not work? Anyway have any ideas why & how? Here is the source, <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3
3971
by: jab3 | last post by:
Hello. I"m new to this group, and to JavaScript in general, so please forgive me if I breach local etiquette. I'm trying to implement some client-side 'dynamic' validation on a form. I'm having a couple of problems, which I'll try to describe. But this e-mail will only reproduce one of them, in a "short" example. What I'm generally doing is having each form entry contained in a div, which as a label, an input with some event handlers,...
3
2814
by: vivela | last post by:
hi, I hope you could help me out on this one,cause I have been trying for 4 days to solve it,but couldn't quite get it right: I have an ASP textbox that should change the value in a dropdownlist. this happens,but the problem is with the validation of the ddl.it has a requiredfieldvalidator,and the error message disapears only if i select a value from the dropdownlist,even though it should also disapear when I write a valid name in the...
4
13445
by: planb | last post by:
Hi, I'd like to have a rollover like effect when a input field has the focus, any idea of how to do this with just CSS (easy enough with javascript)? What I'm thinking of is having a tips box show up next to the input field, but only when the field actually has focus. I was thinking if there's any way to make the focus apply to a
2
3410
dlite922
by: dlite922 | last post by:
Before traversing my code, here's what my goal is and what this function does: I have a table of fields that dynamically grows as the user enters information. A minimum of 3 rows must always exist. (read the psedo code and comment if you need to know what it does) disregard the debugging , commented alerts. What i'm trying to do is without passing the ID or field that called this function, set the focus to the next element. what's...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.