473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

show or hide fields using a checkbox

there is one check box and if it is checked, then shows several fields
underneath; otherwise, several fields are hidden. the default is
unchecked.

anyone can share the code for this function?

<table>
<tr><td><input type="checkbox" name="vehicle" value="N" /><td></tr>
<tr><td><input type="text" name="color" value="test" /><td></tr>
<tr><td><input type="text" name="shape" value="test" /><td></tr>
</table>

Mar 13 '07 #1
2 8834
TGEAR scribed:
>there is one check box and if it is checked, then shows several fields
underneath; otherwise, several fields are hidden. the default is
unchecked.

anyone can share the code for this function?

<table>
<tr><td><input type="checkbox" name="vehicle" value="N" /><td></tr>
<tr><td><input type="text" name="color" value="test" /><td></tr>
<tr><td><input type="text" name="shape" value="test" /><td></tr>
</table>
One method would be to wrap the form input elements in a div and use
javascript to change the CSS display style:

function toggle(what,targetId) {
target = document.getElementById(targetId);
target.style.display = (what.checked)?'':'none';
}
<input type="checkbox" name="vehicle" value="N"
onClick="toggle(this,'divtochange');"/>

<div id="divtochange" style="display:none;">
<input type="text" name="color" value="test" />
<input type="text" name="shape" value="test" />
</div>
--
Ed Jay (remove 'M' to respond by email)
Mar 13 '07 #2
TGEAR wrote:
>here is one check box and if it is checked, then shows several fields
underneath; otherwise, several fields are hidden. the default is
unchecked.

anyone can share the code for this function?
Personally, I favour the approach of using CSS, with nested containers
with their own classes. Just changing the class (className in
Javascript) of the parent container can completely control what child
elements are visible, based on their class or id.
><table>
<tr><td><input type="checkbox" name="vehicle" value="N" /><td></tr>
<tr><td><input type="text" name="color" value="test" /><td></tr>
<tr><td><input type="text" name="shape" value="test" /><td></tr>
</table>
You can use the onchange or onclick event of one of the checkboxes, and
change the class of the table, for example. The showing/hiding logic is
then completely handled by CSS, while the script logic is reduced to as
little as possible.

I'm just making this up as we go along:

<style type="text/css">
#parentTable.vehicleOn .showOff, #parentTable.vehicleOff .showOn {
display: none;
}
</style>
<table id="parentTable" class="vehicleOff">
<tr><td><input type="checkbox" name="vehicle" value="N"
onclick="document.getElementById('parentTable').cl assName = this.checked
? 'vehicleOn' : 'VehicleOff'"/></td></tr>
<tr class="showOn"><td><input type="text" name="color" value="color
test" /></
td></tr>
<tr class="showOff"><td><input type="text" name="shape" value="shape
test" /></td></tr>
</table>

You can pull the code out of the onclick even and make it a function to
call.

--
Bart.
Mar 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: sergio | last post by:
Hi all, I have created the following script that will show/hide a menu based on checkboxes. It works fine in Opera but not on IE6! Does anybody knows a workaround this problem? Thanks for your...
1
by: DettCom | last post by:
Hello all, I saw this post on google that contained the following code and I tried it but it did not work. I am trying create two contact forms and it the info in the first form is the same...
2
by: MOHSEN KASHANI | last post by:
Hi, I am trying to hide some form elements in a form by default and show/hide depending on which radio button is clicked. This is what I have but it is not working: <head> <style> ..noshow {...
4
by: jerryyang_la1 | last post by:
I've found this script that allows be to hide/show form elements.. <script language="JavaScript"><!-- var toggle = true; function show(object) { if (document.layers && document.layers)...
2
by: UJ | last post by:
Is there a way with a asp:checkbox to run a JavaScript to display/hide text/input on the screen without doing a postback? I also need to be able to access the stuff at the server so I need to...
1
by: asilverpeach | last post by:
Hey Guys! Found some great scripts here on this topic but have to make to changes to the code that I can't seem to figure out. First, In the following code clicking on the headers shows the...
13
by: rupak | last post by:
Hi! I have a multiple selection of checkboxes <input type='checkbox' name='coffee' id='col1' value='Address'/> <input type='checkbox' name='coffee' id='col2' value='Name'/> <input...
1
by: filch | last post by:
Hi, I am new to this forum ... so hello to all! I am trying to get a script working which will show or hide a table based on a user checking or unchecking a parent checkbox. This is the...
0
Frinavale
by: Frinavale | last post by:
This code snippet is just a little bit of fun. It demonstrates how you use JavaScript and CSS to show or hide columns in a table depending on whether or not a checkbox corresponding with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.