473,625 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Text visible only when webform field selected

5 New Member
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 1944
gits
5,390 Recognized Expert Moderator Expert
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
tromton
8 New Member
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
Josh Mitchell
5 New Member
Many thanks Tromton....work s 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 Recognized Expert Moderator Expert
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_onf ocus(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
Josh Mitchell
5 New Member
Thank you both! It is much appreciated.
Josh
Dec 17 '07 #6
gits
5,390 Recognized Expert Moderator Expert
aaargh ... sorry for misguiding you :) ... this is the textbox of course and you should use the document.getEle mentById('usern ote') 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
1571
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
14910
by: jmarr02s | last post by:
Hi, How do I replace with when the field is empty? Thanks! jmarr02s
3
1760
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 answers a question by typing in a text box (2) a response is then written below the form, saying whether the answer is right or wrong and, perhaps making a comment of some kind (3) the user's answer remains visible in same text box as it was...
5
3223
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 used access and need some refreshing. All help is appreciated
2
5065
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: Private Sub Memo_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
8
15246
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. How can I do it from my code or should I place the condition if ever? thanks, DM echo '<tr><td><b>Lead Generated By:</b></td>'; $res=mysql_query("select lead from tblgenerated"); if(mysql_num_rows($res)==0){ echo "there is no data in...
3
3223
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 the First Name and the Last Name separated by a space. The text box only shows the First Name and not the last Name. Any idea how to show the complete field value? <% Response.Expires = 0 Response.Cookies ("name")= "" Response.Cookies ("Eid")=...
6
3612
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 soon as they either click on the box or start typing. Or is it possible (which is what I really want) to have a drop down list that says --Select One-- before they exapnd it, but when they expand it, that option is availble to be selected?
1
2727
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. because the clients scan the number in two different ways. Either using the box or the label on item. if they scan through the box, there will be no leading zero. But if the same item scanned through the label there is leading o. For instance if they...
0
8688
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7178
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.