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>

Tobbe070599Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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>
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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Tobbe070599Author Commented:
Now it works in IE but it doesn't work in Firefox 2.0. Hmmm...
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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

Tobbe070599Author Commented:
YES!!!

Thanks a lot IPxtech, now it works perfectly!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.