RSS Forums RSS

onclick text box?

Please support our JavaScript / DHTML / AJAX advertiser: Programming Forums
Reply
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 0
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

onclick text box?

  #1  
Dec 1st, 2008
Hopefully this is the correct place to post this, so here ya go. I'm learning HTML and just had a few questions. I was using some tutorials from w3schools.com, and I came across Event Attributes. So I have this textbox, and I want it, when clicked, to clear the the text box of it's value, and change the text color to black (as in the actual text box, not the whole screen). Then when you click out of the text box, I want it to change the text color back to grey and have the value of the text box be 'Email' again. Here's the code:

  1. <form>
  2. <input type="text" value="Email" name="Email"
  3. style="color:grey;text-align:left;background:white">
  4. </form>

I realize this must use DHTML, but I am unable to find a good source to help me with this problem. Thanks!
Last edited by peter_budo : Dec 6th, 2008 at 4:27 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 271
Reputation: Luckychap is an unknown quantity at this point 
Solved Threads: 35
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz in Training

Re: onclick text box?

  #2  
Dec 1st, 2008
You have to write a handlers for onfocus and onblur events for this textbox like this:

  1.  
  2. <form>
  3. <input type="text" value="Email" name="Email" style="color:grey;text-align:left;background:white" onfocus='changeTextColor(this, 1)' onblur='changeTextColor(this, 0)'>
  4. </form>
  5.  

And your handlers will look like this:

  1. function changeTextColor(tBox, c) {
  2. if(c==1) { //textbox on focus
  3. tBox.style.color = "black";
  4. } else { //textbox out of focus
  5. tBox.style.color = "grey";
  6. }
  7. }
Last edited by Luckychap : Dec 1st, 2008 at 11:28 pm.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Posts: 730
Reputation: essential is on a distinguished road 
Solved Threads: 104
Featured Poster
Featured Coder
essential's Avatar
essential essential is online now Online
Master Poster

Re: onclick text box?

  #3  
Dec 2nd, 2008
You may also try this to skip the coding...
<form action="#" onsubmit="return false;">
<input type="text" value="Email" name="Email"
style="color:grey;text-align:left;background:white"
onfocus="this.style.color = '#000';"
onblur="this.style.color = '#aaa';" />
</form>
The test of a first-rate intelligence is the ability to hold two opposed ideas in the mind at the same time, and still retain the ability to function. One should, for example, be able to see that things are hopeless and yet be determined to make them otherwise.
Reply With Quote  
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 0
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: onclick text box?

  #4  
Dec 3rd, 2008
thanks! it worked fine, but how would I have it clear the value of the textbox when clicked?
Reply With Quote  
Posts: 730
Reputation: essential is on a distinguished road 
Solved Threads: 104
Featured Poster
Featured Coder
essential's Avatar
essential essential is online now Online
Master Poster

Re: onclick text box?

  #5  
Dec 3rd, 2008
You may also apply this with onfocus event...
  1. <input type="text" value="Enter Some Text!" size="30" onclick="this.value='';" />
Reply With Quote  
Posts: 271
Reputation: Luckychap is an unknown quantity at this point 
Solved Threads: 35
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz in Training

Re: onclick text box?

  #6  
Dec 3rd, 2008
Originally Posted by besktrap View Post
thanks! it worked fine, but how would I have it clear the value of the textbox when clicked?


Now ask us how to hide the text box when clicked.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote  
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 0
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: onclick text box?

  #7  
Dec 4th, 2008
um...ok.
how do you hide the textbox when clicked?
Reply With Quote  
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 0
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: onclick text box?

  #8  
Dec 4th, 2008
so I have these two chunks of code:

  1. <form action="#" onsubmit="return false;">
  2. <input type="text" value="Email" name="Email"
  3. style="color:grey;text-align:left;background:white"
  4. onfocus="this.style.color = '#000';"
  5. onblur="this.style.color = '#aaa';" />
  6. </form>

&

<input type="text" value="Enter Some Text!" size="30" onclick="this.value='';" />

How would I combine the two?
Last edited by peter_budo : Dec 6th, 2008 at 4:28 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote  
Posts: 730
Reputation: essential is on a distinguished road 
Solved Threads: 104
Featured Poster
Featured Coder
essential's Avatar
essential essential is online now Online
Master Poster

Re: onclick text box?

  #9  
Dec 4th, 2008
Break the code by using (;), and your code should be like this -->
  1. <form action="*" onsubmit="return false;" name="myForm">
  2. <input type="text" name="myText" value="Enter some text!" onclick="this.value='';this.style.visibility='hidden';" size="30" />&nbsp;
  3. <input type="button" name="myButton" value="Show Me!" onclick="document.myForm.myText.style.visibility='visible';" />
  4. </form>
Hope it clear's you up! Enjoy...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Views: 3207 | Replies: 8 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:17 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC