views:

241

answers:

4

i have the following function

 function change() 
            {
                var input=document.getElementById('pas');
                var input2= input.cloneNode(false);
                input2.type='password';
                input.parentNode.replaceChild(input2,input);
                input2.focus();
            }

but focus() doesn't work in ie7, so what can i do! i want to have the cursor inside of input!

thanks

update

great solution, thanks, but now it doesn't work in opera:(

A: 

IE7 does not support the focus() method. I don't see any method.

Kangkan
@Kangkan maybe via javascript or jquery i can rich it?
Syom
@Syom: I tried Javascript, but not the way shown by @zaf.@zaf: thanks. A nice way to go.
Kangkan
+7  A: 

For IE you need to use a settimeout function due to it being lazy, for example:

setTimeout(function() { document.getElementById('myInput').focus(); }, 10);

From http://www.mkyong.com/javascript/focus-is-not-working-in-ie-solution/

For opera, this may help: http://stackoverflow.com/questions/1431802/how-to-set-focus-in-required-index-on-textbox-for-opera

zaf
@zaf yes man, it works fine;)but now i've notised, that it doesn/t work in opera:)
Syom
Updated my answer with a possible solution. Let us know if it works for you.
zaf
+1  A: 

Its is very easy using jQuery, not sure why you are doing it the hard way :) In this example I have a class assigned to the input field I want the initial focus set called initFocus. You can use any selector you want to find your element. from your code I would use $("#pas").focus();

$(".initFocus").focus();
Chris Love
A: 

I've had the same issue and was able to get IE to work using code behind by making a SetInitialFocus function and calling it in my PageLoad function.

Take a look at the following example and give it a shot, it worked for me. http://www.cambiaresearch.com/c4/df9f071c-a9eb-4d82-87fc-1a66bdcc068e/Set-Initial-Focus-on-an-aspnet-Page.aspx

markiyanm