tags:

views:

2483

answers:

6

When ever I do onchange event, its going inside that function its validating, But focus is not comming I am using document.getElementById('controlid').focus();

I am using Mozilla Firefox and Google Crome, in both its not working. I don't want any IE. Can any one tell me what could me the reason.

Thanks in advance

Here's the code:

var mnumber = document.getElementById('mobileno').value; 
if(mnumber.length >=10) {
    alert("Mobile Number Should be in 10 digits only"); 
    document.getElementById('mobileno').value = ""; 
    document.getElementById('mobileno').focus(); 
    return false; 
}
+1  A: 

Try using the jquery library, then you can do the following which should work in all browsers:

$('#mobileno').focus();

I used this today in Chrome so I know it works there. This one thing may not be enough on its own to justify adding a whole library to your javascript code base, but I think you'll find it useful in many other ways this one just being the first.

To use jquery, include the jquery library at the top of you javascript references:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
crizCraig
Thanks for ur good suggestion, but I don't no how to use jquery library in my application, actually I am using simple html and javascript. can u sujest me how to use jquery librariesThanks in advance
SKSK
I'm not a jquery user, but surely $('#mobileno').focus() is the same as document.getElementById('mobileno').focus()? In which case, jquery would provide no benefit here.
Andy E
Using jquery is just a matter of including a javascript file. Jquery.com actually uses google to host it like this:<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> Alternatively, you can download the file from jquery.com and host it on your own server.
crizCraig
I was also having issues getting focus() to work in Chrome -- using jQuery fixed it for me.
shedd
A: 

I also recomend you jQuery's focus() function. Your life will become simpler :) Some time ago I got problem with focus (w/o jQuery) and I gave up.

Thinker
+4  A: 

Try using a timer:

var mnumber = document.getElementById('mobileno').value;
if (mnumber.length >=10) 
{ 
    alert("Mobile Number Should be in 10 digits only");
    document.getElementById('mobileno').value = "";
    window.setTimeout(function ()
    {
        document.getElementById('mobileno').focus();
    }, 0);
    return false;
}

A timer with a count of 0 will run when the thread becomes idle. If that doesn't help, try the code (with the timer) in the onblur event instead.

Andy E
Thanks its working ..Thanks a lot..
SKSK
A: 

Are you trying to reference the element before the DOM has finished loading?

Your code should go in body load (or another function that should execute when the DOM has loaded, such as $(document).ready() in jQuery)

body.onload = function() { 
    // your onchange event handler should be in here too   
    var mnumber = document.getElementById('mobileno').value; 
    if(mnumber.length >=10) {
        alert("Mobile Number Should be in 10 digits only"); 
        document.getElementById('mobileno').value = ""; 
        document.getElementById('mobileno').focus(); 
        return false; 
    }    
}
Russ Cam
A: 

Make sure you use "id" and "getElementById" as shown here:

<FORM id="my_form" name="my_form" action="">
<INPUT TYPE="text" NAME="somefield" ID="somefield" onChange="document.getElementById('my_form').submit();">
</FORM>
Westchaser
A: 

The timeout suggested above worked around the issue on Chrome. Not issue existed on IE8 or FF3. Thanks for the tip as always. My code that to set focus that worked is: window.setTimeout(function() { document.form1.password.focus(); },0);

Don