odd behavoir of alert in FF

M

Michael

can anyone explain why I get an endless loop of alerts (alert - hit enter -
alert - hit enter etc) from the following script/page?
It works ok in IE, and works ok in FF if I use the mouse to click the "ok",
but not if I use the enter key....

<html>
<head>
<title>Test</title>

<script type="text/javascript">
function updateForm(dom)
{
var qty = document.getElementById(dom).value;

if (qty != "")
if (qty == 0 || qty > 5)
{
alert ("The number may not be 0 and not bigger than 5!");
document.getElementById(dom).select();
document.getElementById(dom).focus();
}
}
</script>
</head>

<body>
<form action="#">
Enter a number:
<!-- I changed value="0" to value="" -->
<td> <input type = "text" name = "qty1" id = "qty1" value="" size="15"
onkeyup = "updateForm('qty1');" /> </td>

</form>
</body>
</html>

regards

Michael
 
R

Randy Webb

Michael said the following on 11/7/2006 7:30 PM:
can anyone explain why I get an endless loop of alerts (alert - hit enter -
alert - hit enter etc) from the following script/page?
It works ok in IE, and works ok in FF if I use the mouse to click the "ok",
but not if I use the enter key....

Because the Enter key triggers the onkeyup for the input (which is
incorrect behavior to me).

Why are you passing a string that is the ID of the input and then
looking up that input with gEBI rather than just passing a reference to
the field and it's value?
 
M

Michael

Randy Webb said:
Michael said the following on 11/7/2006 7:30 PM:

Because the Enter key triggers the onkeyup for the input (which is
incorrect behavior to me).

Why are you passing a string that is the ID of the input and then looking
up that input with gEBI rather than just passing a reference to the field
and it's value?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/

Thanks for the reply Randy.

This was actually written by a fellow student, rather than me. I couldn't
figure out why it was behaving as it was and our tutor wasn't helping much
so I thought I'd ask here. I think this is probably a much cut own version
of a larger script, hence it seems a bit round about in its method.

So how does the keyup on the alert object cause the firing of the event
registered to the <input> object? Why are they not separate?

Thanks for your help

Regards

Michael
 
B

buhailiang

it seems that when alert object receive onkeydown, it closed, and so
onkeyup is send to the input object.

"Michael дµÀ£º
 
L

Lee

Michael said:
can anyone explain why I get an endless loop of alerts (alert - hit enter -
alert - hit enter etc) from the following script/page?
It works ok in IE, and works ok in FF if I use the mouse to click the "ok",
but not if I use the enter key....

<html>
<head>
<title>Test</title>

<script type="text/javascript">
function updateForm(dom)
{
var qty = document.getElementById(dom).value;

if (qty != "")
if (qty == 0 || qty > 5)
{
alert ("The number may not be 0 and not bigger than 5!");
document.getElementById(dom).select();
document.getElementById(dom).focus();
}
}
</script>
</head>

<body>
<form action="#">
Enter a number:
<!-- I changed value="0" to value="" -->
<td> <input type = "text" name = "qty1" id = "qty1" value="" size="15"
onkeyup = "updateForm('qty1');" /> </td>

</form>
</body>
</html>


Performing an action on a key-up event is risky in any GUI
language because you can never be sure that the corresponding
key-down occurred in the same focus. That seems to be the
problem here. The keydown event is seen by the alert window
and the keyup is seen by the input field.

Instead of "onkeyup", use "onkeypress" or "onchange".


--
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top