[SOLVED] Changing onfocus of a textbox

This is a discussion on "[SOLVED] Changing onfocus of a textbox" within the JavaScript Forum section. This forum, and the thread "[SOLVED] Changing onfocus of a textbox are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jan 2nd, 2008, 11:26
Junior Member
Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Changing onfocus of a textbox

I'm trying to learn javascript at the moment, but I can't figure out why the below code doesn't work. The page has a textbox you can't type in by using onfocus="blur()", then click the button to make onfocus="focus()" so you can type in it, then click again to change it back to onfocus="blur()". But it doesn't work. Can anyone tell me what I've done wrong?

Thanks

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
var i=0;
function typejim()
{
    if (i==0)
    {
        window.document.form1.textbox1.onfocus="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="blur()";
        window.document.form1.textbox1.value="you can't type in me!";
        window.document.form1.typebutton.value="type";
        i--;
    }
    
}
</script>
</head>

<body>
<form name="form1">
<input type="text" name="textbox1" value="you can't type in me!" onfocus="blur()">
<input type="button" value="type" name="typebutton" onclick="typejim()">
</form>
</body>
</html>
Reply With Quote

  #2 (permalink)  
Old Jan 5th, 2008, 03:00
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Changing onfocus of a textbox

OK, this should be accomplished by disabling the text box. Here is the entire code:
HTML: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<script language="javascript" type="text/javascript">
var i=0;
function typejim()
{
    if (i==0)
    {
        window.document.form1.textbox1.value="";
        window.document.form1.textbox1.disabled=false;
        window.document.form1.typebutton.value="Cancel";
        i++;        
    }
    else if (i==1)
    {
        window.document.form1.textbox1.value="You can't type in me!";
        window.document.form1.textbox1.disabled=true;
        window.document.form1.typebutton.value="type";
        i--;
    } 
}
</script>
</head>

<body>
<form name="form1" action="">
<input type="text" name="textbox1" value="You can't type in me!" disabled="disabled">
<input type="button" value="type" name="typebutton" onClick="typejim()">
</form>
</body>
</html>
That should do it!

Cheers
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)

Last edited by Stuart; Jan 5th, 2008 at 03:06.
Reply With Quote
  #3 (permalink)  
Old Jan 5th, 2008, 08:29
Junior Member
Join Date: Oct 2007
Location: UK
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Changing onfocus of a textbox

Thanks for your help, that works well.

I also got it to work in the original way I was trying, although I think I need to learn quite a bit more before I understand it.
Reply With Quote
  #4 (permalink)  
Old Jan 5th, 2008, 13:01
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [SOLVED] Changing onfocus of a textbox

How did you solve it with the original? Then, I can explain it to you.
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Reply With Quote
  #5 (permalink)  
Old Jan 5th, 2008, 15:32
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
  #6 (permalink)  
Old Jan 5th, 2008, 17:38
Highly Reputable Member
Join Date: Sep 2007
Age: 15
Posts: 717
Blog Entries: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: [SOLVED] Changing onfocus of a textbox

OK. First of all, about the IE thing, your code is a bit strange and IE is picky with stuff like that.

Now to explain:
1. focus() is a function that puts the cursor into the textbox automatically. It doesn't disable the textbox like you wanted.
2. It's not usually a good idea to call functions in that particular area. It might be throwing errors.
3. In javascript, you always need to specify the exact element so that it works correctly.

So, I suggest using my idea, since you run into less problems, and the code is cleaner.

Cheers
Last Blog Entry: Windows Vista vs. Mac Leopard (Nov 4th, 2007)
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Change Input Textbox Background Color tejaxx JavaScript Forum 4 Nov 19th, 2007 20:16
[SOLVED] Changing Colours crackafaza PHP Forum 37 Oct 25th, 2007 11:55
[SOLVED] Changing from html to CSS mcdanielnc89 Web Page Design 16 Oct 11th, 2007 04:29
[SOLVED] Changing pictures on same page R8515198 JavaScript Forum 7 Oct 3rd, 2007 00:14
events in NS (onfocus, onblur) spinal007 Web Page Design 6 Apr 27th, 2004 16:32


All times are GMT. The time now is 04:50.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43