• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1879
  • Last Modified:

Show & hide text onfocus on onblur

Hello

I'm trying to get a information text to show when a form field is onfocus and then change this text when another field is focused to give the user information how to fill out the form.

I want the text to show in a table to the right of the form, all texts at the same position.

I found examples on drop down texts but I don't want that, I want the text to be replaced at the same position in a specific table every time.

Below is a code I found and I almost got it to work but there are a few problems.
1) It only works in IE and not in Firefox 2.0
2) The text is not shown in the exakt same position due to the divs taking place even though they are not visible.


<html>
      <body>
            <script>
            <!-- Begin
            // quick browser tests
            var ns4 = (document.layers)? true : false;
            var ie4 = (document.all &&!document.getElementById)? true : false;
            var ie5 = (document.all && document.getElementById)? true : false;
            var ns6 = (!document.all && document.getElementById)? true : false;

            function show(sw,obj) {
            // show/hide divs
            if (sw && ie5) document.all[obj].style.visibility = 'visible';
            if (!sw && ie5) document.all[obj].style.visibility = 'hidden';
            }

            function poptextfield(what,obj){
            document.getElementById(what).value=obj;
            }

            // End -->
            </script>

            <style type="text/css">
            <!--
            .myLayersClass { position: relative; visibility: hidden; }
            //-->
            </style>


            <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="57%">
                  <tr>
                        <td>
                              Field1:<br>
                              <input type="text" name="textfield_1" id="textfield_1" size="25" borders="0" onfocus="show(true,'div1');" onblur="show(false,'div1');" ><br><br>

                              Field2:<br>
                              <input type="text" name="textfield_2" id="textfield_2" size="25" borders="0" onfocus="show(true,'div2');" onblur="show(false,'div2');" ><br><br>

                              Field3:<br>
                              <input type="text" name="textfield_3" id="textfield_3" size="25" borders="0" onfocus="show(true,'div3');" onblur="show(false,'div3');" >
                        </td>
                        <td>
                              <div id = "div1" class = "myLayersClass">
                                    Text 1
                              </div>
                              <div id = "div2" class = "myLayersClass">
                                    Text 2
                              </div>
                              <div id = "div3" class = "myLayersClass">
                                    Text 3
                              </div>                  
                        </td>
                  </tr>
            </table>

      </body>
</html>

0
Tobbe070599
Asked:
Tobbe070599
  • 4
  • 3
1 Solution
 
lpxtechCommented:
Hi,

You could use display: none; and display: block; rather than visibility: hidden; and visibility: visible; visibility only hides but keeps the height in place. display none; will hide it completely.
0
 
Tobbe070599Author Commented:
I delete some of the code that seemed to do nothing...
I still can't get display:none to work.


<html>
      <body>
            <script>
            <!-- Begin

            function show(sw,obj) {
            // show/hide divs
            if (sw) document.all[obj].style.visibility = 'visible';
            if (!sw) document.all[obj].style.visibility = 'hidden';
            }

            // End -->
            </script>

            <style type="text/css">
            <!--
            .myLayersClass { position: relative; visibility: hidden; }
            //-->
            </style>


            <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="57%">
                  <tr>
                        <td>
                              Field1:<br>
                              <input type="text" name="textfield_1" id="textfield_1" size="25" borders="0" onfocus="show(true,'div1');" onblur="show(false,'div1');"><br><br>

                              Field2:<br>
                              <input type="text" name="textfield_2" id="textfield_2" size="25" borders="0" onfocus="show(true,'div2');" onblur="show(false,'div2');"><br><br>

                              Field3:<br>
                              <input type="text" name="textfield_3" id="textfield_3" size="25" borders="0" onfocus="show(true,'div3');" onblur="show(false,'div3');">
                        </td>
                        <td>
                              <div id = "div1" class = "myLayersClass">
                                    Text 1
                              </div>
                              <div id = "div2" class = "myLayersClass">
                                    Text 2
                              </div>
                              <div id = "div3" class = "myLayersClass">
                                    Text 3
                              </div>                  
                        </td>
                  </tr>
            </table>

      </body>
</html>
0
 
lpxtechCommented:
Hi,

This should work. Your html was a bit off so I corrected it.
<!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>My Webpage</title>
<script type="text/javascript">
            function show(sw,obj) {
            // show/hide divs
            if (sw) document.all[obj].style.display = 'block';
            if (!sw) document.all[obj].style.display = 'none';
            }
</script>
<style type="text/css">
.myLayersClass {
	position: relative;
	display: none;
}
</style>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="57%">
	<tr>
		<td> Field1:<br>
			<input type="text" name="textfield_1" id="textfield_1" size="25" borders="0" onfocus="show(true,'div1');" onblur="show(false,'div1');">
			<br>
			<br>
			Field2:<br>
			<input type="text" name="textfield_2" id="textfield_2" size="25" borders="0" onfocus="show(true,'div2');" onblur="show(false,'div2');">
			<br>
			<br>
			Field3:<br>
			<input type="text" name="textfield_3" id="textfield_3" size="25" borders="0" onfocus="show(true,'div3');" onblur="show(false,'div3');">
		</td>
		<td><div id = "div1" class = "myLayersClass"> Text 1 </div>
			<div id = "div2" class = "myLayersClass"> Text 2 </div>
			<div id = "div3" class = "myLayersClass"> Text 3 </div></td>
	</tr>
</table>
</body>
</html>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Tobbe070599Author Commented:
Now it works in IE but it doesn't work in Firefox 2.0. Hmmm...
0
 
lpxtechCommented:
I'm sorry, I didn't actually look at the javascript too closely except for the styling.

Just use document.getElementById
function show(sw,obj) {
      // show/hide divs
      if (sw) document.getElementById(obj).style.display = 'block';
      if (!sw) document.getElementById(obj).style.display = 'none';
}

Open in new window

0
 
lpxtechCommented:
also, I wouldn't put spaces between id and = etc. see code below.
Original:
<div id = "div1" class = "myLayersClass"> Text 1 </div>
                        <div id = "div2" class = "myLayersClass"> Text 2 </div>
                        <div id = "div3" class = "myLayersClass"> Text 3 </div></td>
 
 
Desired:
<div id="div1" class="myLayersClass"> Text 1 </div>
                        <div id="div2" class="myLayersClass"> Text 2 </div>
                        <div id="div3" class="myLayersClass"> Text 3 </div></td>

Open in new window

0
 
Tobbe070599Author Commented:
YES!!!

Thanks a lot IPxtech, now it works perfectly!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now