View Single Post
  #5 (permalink)  
Old Jan 5th, 2008, 15:32
djeyewater djeyewater is offline
Junior Member
Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [SOLVED] Changing onfocus of a textbox

Thanks, here's the javascript.

Code: Select all
var i=0;

function typejim()
{
    if (i==0)
    {
        window.document.form1.textbox1.onfocus=function myfocus(){window.document.form1.textbox1.focus();}
        window.document.form1.textbox1.value="You can type in me";
        window.document.form1.typebutton.value="Cancel";
        i++;
    }
    else if (i==1)
    {
        window.document.form1.textbox1.onfocus=function myfocus(){window.document.form1.textbox1.blur();}
        window.document.form1.textbox1.value="you can't type in me!";
        window.document.form1.typebutton.value="type";
        i--;
    }
    
}
It doesn't work in IE but worked okay in Opera, FF & Safari. In IE setting onfocus=blur() doesn't seem to work. This is only for practice, not anything practical so I'm not too bothered if it doesn't work in IE, although it would be useful to know why it doesn't work.

What I don't understand about it is:
1. Why didn't the original function (setting window.document.form1.textbox1.onfocus=focus()) work?
2. Why does it only work if the function myfocus is declared while setting onfocus? If I change the myfocus function to a seperate function and then just call window.document.form1.textbox1.onfocus = myfocus() it doesn't work.
3. In the myfocus function it must refer to the element you want focused, I don't understand why? (I couldn't get it to work using self.focus() either).

If you're able to explain any of things to me that would be great!

Thanks
Reply With Quote