Solved

Show text if checkbox ticked (cross browser solution) PHP

Posted on 2006-06-16
9
196 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
ID: 16925278
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
ID: 16925285
Fantastic!  Thanks.  Worked like a charm!

Debbie
0
 
LVL 10

Expert Comment

by:js_vaughan
ID: 16925290
Glad to be of help :-)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 
LVL 1

Author Comment

by:debbieau1
ID: 16925302
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
 
LVL 10

Expert Comment

by:js_vaughan
ID: 16925323
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
ID: 16925328
Yes, that would be ideal, then I could work out a lot myself from there.
0
 
LVL 10

Expert Comment

by:js_vaughan
ID: 16925370
<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
ID: 16925377
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
ID: 16925383
Glad to be here.  Cheers!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

828 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