Solved

Show text if checkbox ticked (cross browser solution) PHP

Posted on 2006-06-16
9
193 Views
Last Modified: 2006-11-18
Hi

I need  to have a checkbox, and when the user checks this, then it will display other information (but only show if the box is checked by the user)

I did a search a found a solution, but it did not work in Netscape, only in IE and Mozilla.  I do need it to work in Netscape.

Could someone please tell me how to do this so that it will work in all major browsers.

Thanks

Debbie
0
Comment
Question by:debbieau1
  • 5
  • 4
9 Comments
 
LVL 10

Accepted Solution

by:
js_vaughan earned 500 total points
Comment Utility
Debbie,

I have tested this to work with IE, Netscape, Firefox, and Opera....


<html>
<head>
<title>Experts-Exchange</title>

<script language="javascript">
<!--

function toggleText(input){

    var block = document.getElementById("hiddentext");

    if (input.checked) {
        block.style.display = "block";
    } else {
        block.style.display = "none";
    }
}

// -->
</script>

</head>
<body>

<form name="your_Form">

    <input type="checkbox" name="togglebox" onClick="toggleText(this)"> Show Hidden Text
    <br>

    <p>I will always be visible.</p>
   
    <div id="hiddentext" style="display:none;">
        <p>I am hidden most of the time.</p>
    </div>

</form>

</body>
</html>
0
 
LVL 1

Author Comment

by:debbieau1
Comment Utility
Fantastic!  Thanks.  Worked like a charm!

Debbie
0
 
LVL 10

Expert Comment

by:js_vaughan
Comment Utility
Glad to be of help :-)
0
 
LVL 1

Author Comment

by:debbieau1
Comment Utility
I hope you don't mind if I ask one more question on this.  I know you have answered the question for me, but if it's not too much trouble could you tell me how to use multiple checkboxes on the one page, if it's not too much trouble
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 10

Expert Comment

by:js_vaughan
Comment Utility
What's an example of your scenario?  For example, do you need 1 checkbox to control 1 hidden block, and a second checkbox to control a second block?
0
 
LVL 1

Author Comment

by:debbieau1
Comment Utility
Yes, that would be ideal, then I could work out a lot myself from there.
0
 
LVL 10

Expert Comment

by:js_vaughan
Comment Utility
<html>
<head>
<title>Experts-Exchange</title>

<script language="javascript">
<!--

function toggleText(input){

    var block;
   
            // Determine the correct block to adjust by looking at the checkbox's ID
    switch (input.id) {
   
        case "toggle1":
            block = document.getElementById("hiddentext1");
            break;
        case "toggle2":
            block = document.getElementById("hiddentext2");
            break;
        default:
            alert('Error in input.id');
    }

    // Adjust the block based on checked / unchecked
    if (input.checked) {
        block.style.display = "block";
    } else {
        block.style.display = "none";
    }
}

// -->
</script>

</head>
<body>

<form name="your_Form">

    <p>I will always be visible.</p>

    <input type="checkbox" name="togglebox" id="toggle1" onClick="toggleText(this)"> Show Block 1
    <br>
   
    <div id="hiddentext1" style="display:none;">
        <p>I am  block 1.</p>
    </div>
            
    <input type="checkbox" name="togglebox" id="toggle2" onClick="toggleText(this)"> Show Block 2
    <br>
   
    <div id="hiddentext2" style="display:none;">
        <p>I am block 2.</p>
    </div>

</form>

</body>
</html>
0
 
LVL 1

Author Comment

by:debbieau1
Comment Utility
You're an absolute star!  Thank you works great.  I really appreciate you giving the extra help, especially considering I had already accepted your first answer.

Thanks again, this is such a help.
0
 
LVL 10

Expert Comment

by:js_vaughan
Comment Utility
Glad to be here.  Cheers!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now