Celeritas
Celeritas

Reputation: 15053

Why does this event keep getting fired?

This script checks to see if the the form has been filled in correctly but the toggle() function keeps getting fired (at least in FireFox). In the following example "now here" keeps showing up after the form is valid (that is all fields have some text).

<script type="text/javascript">
        <!--
        function toggle()
        {
            if(document.getElementById('username').value.length > 1 && document.getElementById('pw1').value.length > 1 && document.getElementById('pw2').value.length > 1 && (document.getElementById('pw1').value == document.getElementById('pw2').value))
            {
                alert("now here")
                document.getElementById('submit').disabled = false
            }
        }
//-->
</scipt>
Username:<input type="text" name='username' id='username' maxlength="30" onkeyup="toggle()"/><br />
        Password:<input type="password" name="pw1" id="pw1" onkeyup="passwordCheck(document.getElementById('pw1'), document.getElementById('pw2')); toggle()"/><br />
        Confirm password:<input type="password" name="pw2" id="pw2" onkeyup="passwordCheck(document.getElementById('pw1'), document.getElementById('pw2')); toggle()"/>

I'm woundering why does onkeyup keep getting fired? It's as if the user never takes there finger off the key. Is it because onkeyup gets screwed up when it tirggers an alert window because the focus changes from the text field to the window and when ok is pressed the focus changes back to the text field?

UPDATE: I found it here in Bugzilla. You can vote for it. It's been reported since late 2001, why has it bean so long and not fixed???

Upvotes: 0

Views: 193

Answers (3)

Geoff
Geoff

Reputation: 5423

I got around this issue by using onkeydown instead of onkeyup. YMMV.

Upvotes: 0

Celeritas
Celeritas

Reputation: 15053

In FireFox if you press the enter key on an alert window it fires onKeyUp

Upvotes: 1

dmayo
dmayo

Reputation: 640

Looks like you want the function to make the submit change from disabled to enabled when the form is correctly filled out. The function should fire every time the onKeyUp is called with the all three inputs because you are calling it in all three inputs.

My jsfiddle (http://jsfiddle.net/BhaeG/) only fires the alert("now here") when all conditions of the if statement are correct.

Unless I'm really missing something, it seems like it's working as designed.

Upvotes: 1

Related Questions