Jump to content

Archived

This topic is now archived and is closed to further replies.

mb81

problems with .focus()

Recommended Posts

I have a problem with the javascript below. It seems that it will select, but not focus, what am I doing wrong?

[code]
function checkdata(itemname) {
    switch (itemname) {
        case 'emailconfirm' :
            if (document.editprofile.emailconfirm.value != document.editprofile.email.value) {
                alert('The email addresses you have entered do not match');
                document.editprofile.emailconfirm.select();
                document.editprofile.emailconfirm.focus();
            }
        break;
        case 'passwordconfirm' :
            if (document.editprofile.password.value != document.editprofile.passwordconfirm.value) {
                alert('The passwords you have entered do not match.');
                document.editprofile.passwordconfirm.select();
                document.editprofile.passwordconfirm.focus();
            }
        break;
    }
}
[/code]

Share this post


Link to post
Share on other sites
when you run select() on a text input field, the focus is put on that field. you shouldn't have to do focus() after that.

Share this post


Link to post
Share on other sites
[!--quoteo(post=374094:date=May 15 2006, 03:13 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ May 15 2006, 03:13 PM) [snapback]374094[/snapback][/div][div class=\'quotemain\'][!--quotec--]
when you run select() on a text input field, the focus is put on that field. you shouldn't have to do focus() after that.
[/quote]

My cursor is in the next field, I tried it .select, then .focus, I tries it .focus, then .select, or just .select, the cursor always seems to be in the next field.

Share this post


Link to post
Share on other sites
Hi,,

That one works,... tested with both FF & IE
[code]
<html>
<head>
<title>Testing,,</title>
</head>
<body>
<script>
function checkdata(itemname)
{
switch (itemname)
    {
    case 'emailconfirm' :
    if (document.getElementById('emailconfirm').value != document.getElementById('email').value)
        {
        alert('The email addresses you have entered do not match');
        document.getElementById('email').select();
        }
    break;
    case 'passwordconfirm' :
    if (document.getElementById('pwdconfirm').value != document.getElementById('pwd').value)
        {
        alert('The passwords you have entered do not match.');
        document.getElementById('pwd').select();
        }
    break;
    }
}

</script>
enter your mail: <input type="text" id="email"><br>
confirm your mail: <input type="text" id="emailconfirm" onchange="checkdata('emailconfirm');"><br><br>
set a password: <input type="password" id="pwd"><br>
confirm your password: <input type="password" id="pwdconfirm" onchange="checkdata('passwordconfirm');"><br>
<script>
//needed for firefox to reset the form,,
document.getElementById('email').value="";
document.getElementById('emailconfirm').value="";
</script>
</body>
</html>
[/code]
I think your "document.editprofile.emailconfirm" method should be more document.forms['editprofile'].elements['emailconfirm'],,...
I guess the problem is from there,...

I prefer mostly the 'document.getElementById' method, giving an Id to each element needed,,... it gives me less error,,... question of preferences,, :)

l8tr,,

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.