473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Text visible only when webform field selected

Hello,

Could anyone direct me to some sample script that makes a div or text visible only when a webform field in selected. So for example, in the below snippet, I want the text "Your username can only contain letters (A-Z) or numbers (0-9)" to be visible only when the user has selected "field1" in the form. Likewise, when the user leaves "field1," the text should become invisible again.

Many thanks!
Josh

Expand|Select|Wrap|Line Numbers
  1. <div id="username">
  2.     Username
  3.     <p></p>
  4.     <input type="text" id="field1" name="field1" size="20" tabindex="1">
  5.  
  6.     <div id="usernote">
  7.     Your username can only contain letters (A-Z) or numbers (0-9)
  8.     </div>
  9.  
  10. </div>
  11.  
Dec 16 '07 #1
6 1919
gits
5,390 Expert Mod 4TB
hi ...

welcome to TSDN ...

you may use the onfocus and onblur of your textbox events for that purpose. onfocus you should set the div's style display to block and unblur to none ...

kind regards
Dec 16 '07 #2
Expand|Select|Wrap|Line Numbers
  1. <div id="username">
  2.     Username
  3.     <p></p>
  4.     <input type="text" id="field1" name="field1" onblur="document.getElementById('usernote').style.display = 'none'" onfocus="document.getElementById('usernote').style.display = 'block';" size="20" tabindex="1">
  5.  
  6.     <div id="usernote" style="display:none;">
  7.     Your username can only contain letters (A-Z) or numbers (0-9)
  8.     </div>
  9. </div>
  10.  
Dec 16 '07 #3
Many thanks Tromton....works like a charm! I'm trying to now combine this onfocus/onblur function with another onfocus/onblur function. Is it possible to combine two scripts that function on the same trigger event?
Thanks!

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  2.       "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  3. <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" >
  4. <head>
  5.   <meta http-equiv="content-language" content="en" />
  6.   <title>User Registration</title>
  7. <style>
  8. html, body {
  9.   color: #000;
  10.   background-color: #fff;
  11.   font-size:15px;
  12. }
  13.  
  14. form div.active {
  15.   background-color: #98FB98;
  16.   border: 0px solid #8a8;
  17. }
  18.  
  19. #user_registration
  20. {
  21.     border:1px solid #6495ED;
  22.     margin:auto auto;
  23.     margin-top:100px;
  24.     width:500px;
  25.     background-color: #ECF1EF;
  26. }
  27.  
  28. </style>
  29. <script type="text/javascript"><!--
  30. function hasClassName(el,c){
  31.   c=c.replace(/\-/g,'\\-');
  32.   return (new RegExp('(^|\\s)'+c+'(\\s|$)')).test(el.className);
  33. }
  34. function addClassName(el,c){
  35.   if(hasClassName(el,c)) return;
  36.   var curClass=el.className||'';
  37.   el.className=curClass+((curClass.length>0)?' ':'')+c;
  38. }
  39. function removeClassName(el,c){
  40.   var re=new RegExp('\\s*'+c.replace(/\-/g,'\\-')+'\\s*');
  41.   el.className=el.className.replace(re,' ').replace(/^\s*|\s*$/g,'');
  42. }
  43. function highlightElm(el,light){
  44.   if(!el) return;
  45.   if(light) addClassName(el,'active');
  46.   else removeClassName(el,'active');
  47. }
  48. window.onload = function(){
  49.   document.getElementById('field1').onfocus=function(){
  50.     highlightElm(this.parentNode,true);
  51.   }
  52.   document.getElementById('field1').onblur=function(){
  53.     highlightElm(this.parentNode,false);
  54.   }
  55.  }
  56.  
  57. // -->
  58. </script>
  59. </head>
  60. <body>
  61.  
  62. <form method="post" action="" id="user_registration" name="user_registration">
  63. <div id="username">
  64.     Username
  65.     <p></p>
  66.     <input type="text" id="field1" name="field1" onblur="document.getElementById('usernote').style.display = 'none'" onfocus="document.getElementById('usernote').style.display = 'block';" size="20" tabindex="1">
  67.  
  68.     <div id="usernote" style="display:none;">
  69.     Your username can only contain letters (A-Z) or numbers (0-9)
  70.     </div>
  71. </div>
  72.  
  73. </form>    
  74.  
  75.  
  76. </body>
  77. </html>
  78.  
Dec 16 '07 #4
gits
5,390 Expert Mod 4TB
for that purpose create a function that calls what you want and call the function from the event-handlers of your textbox ...:

Expand|Select|Wrap|Line Numbers
  1. function do_onfocus(node) {
  2.     // your current code to call
  3.     node.style.display = 'block';
  4.  
  5.    // put the further calls here
  6. }
  7.  
later on:

[HTML]<input type="text" id="field1" name="field1" onfocus="do_onfocus(this);">[/HTML]
note that i pass the node itself to the function ... it's a little bit more elegant and much shorter :)

kind regards
Dec 17 '07 #5
Thank you both! It is much appreciated.
Josh
Dec 17 '07 #6
gits
5,390 Expert Mod 4TB
aaargh ... sorry for misguiding you :) ... this is the textbox of course and you should use the document.getElementById('usernote') to set the display property but you may use this for the highlighting of the inputbox ...

kind regards
Dec 17 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Chris | last post by:
Hi, I use a datagrid in a webform but the grid is never visible in the browser (only at design-time is it visible). Using it in a Winform : no problem. How come ? Thnx
3
by: jmarr02s | last post by:
Hi, How do I replace with when the field is empty? Thanks! jmarr02s
3
by: flunn | last post by:
I'm a beginner with php. I'm trying to learn it so I can make quizzes and tutorials for my English as a Second Language site. I'm trying to make a form that does the following: (1) the user...
5
by: Sideburns | last post by:
I am trying to use a checkbox to make a textbox visible when checkbox=yes and invisible when the checkbox=no. I am not sure if this is the best way to go about this. It has been a while since I have...
2
by: ACF | last post by:
Hello, I’m trying to format only selected text in an RTF Memo field in Access 2007 I’m able to copy selected text in a Memo field to the Windows clipboard with following code: ...
8
ddtpmyra
by: ddtpmyra | last post by:
This is just a follow-up question above, this time i wanted to have a dependend drop down that when the users selected 'others' for instance another texbox will show-up right next to my drop down. ...
3
by: Indrajit Chakraborty | last post by:
I am developing a simple web site in ASP. I have a web page which has a form, and I want to set the values of two text box to 2 fields selected by a query. One of the fields is a name field which has...
6
KeredDrahcir
by: KeredDrahcir | last post by:
Is there a way to put some default text in a form field that will disappear when the user focuses on it. For example you have a login form and it says E-mail in the E-mail box but this disappears as...
1
by: Hema Selvan | last post by:
I am new to MS Access. I have created a database. One of the field have Item number which has more than 20 digits. First i had the field with Text. I have problem as the Item number is duplicating....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.