Jump to content

problems with .focus()


mb81

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]
Link to comment
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.
Link to comment
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,,
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.