show and hide

S

student4lifer

Could someone show me how to make the "someFunction()" in javascript
so that 'Make' and 'Model' appear only if the 'car' checkbox is
clicked? Thanks.

===========

<table>
<tr>
<td></td>
<td>
<input name="car" value="T" type="checkbox"
onclick="someFunction()"> Car
</td>
</tr>

<tr>
<td>Make:
</td>
<td><input id="made" size="20"></td>
</tr>
<tr>
<td>Model:
</td>
<td><input id="model" size="20"></td>
</tr>

</table>
 
S

Sherm Pendley

Could someone show me how to make the "someFunction()" in javascript
so that 'Make' and 'Model' appear only if the 'car' checkbox is
clicked? Thanks.

JavaScript is off-topic here. Try asking in a JavaScript group, such
as comp.lang.javascript.

sherm--
 
S

student4lifer

JavaScript is off-topic here. Try asking in a JavaScript group, such
as comp.lang.javascript.

sherm--

I got the javascript function() to work on <td> but not on <tr>, once
I moved the <div> outside the <tr>, it stop working. What am I
missing? How would one fix this? Below is what I got. Thanks.

===========


<head>
<script type="text/javascript">
<!--
function show(id, checkbox) {
var visible = (checkbox.checked) ? "block" : "none";
document.getElementById(id).style.display = visible;
}
//-->
</script>
</head>

<table>
<tr>
<td></td>
<td>
<input name="car" value="T" type="checkbox"
onclick="show('div1', this)"> Car
</td>
</tr>
<div id="div1">
<tr>
<td>Make:
</td>
<td><input id="make" size="20"></td>
</tr>
<tr>
<td>Model:
</td>
<td><input id="model" size="20"></td>
</tr>
</div>
</table>
 
N

nice.guy.nige

While the city slept, Sherm Pendley feverishly typed:
JavaScript is off-topic here. Try asking in a JavaScript group, such
as comp.lang.javascript.

From http://www.html-faq.com/alt.html/?ontopic

"Although there are many specialised newsgroups dealing with Javascript,
CGI, stylesheets, webmastering, questions and discussions about it are still
welcome in alt.html. You may get a technically better answer in specialised
newsgroups. Many of the regulars in alt.html are also regulars in these
specialised groups anyway."

So, no. Javascript is not off-topic here.

Cheers,
Nige
 
R

Roy A.

[...]

Get rid of the div. Put the id on the tr instead, and toggle its display
property between "table-row" and "none", rather than between "block" and
"none".

i.e.:

    display = (checkbox.checked) ? "table-row" : "none";
    document.getElementById(id).style.display = display;

IE 6 and IE 7 has no idea of 'table-row', you would be better of using
an empty string. Then IE can pretend you wanted 'display:block', while
normal browsers can assume you wantet 'display:table-row', i.e.:

display = (checkbox.checked) ? "" : "none";
That should work, but I haven't tested it, and nothing I say ever works
in IE.

Internet isn't compatible with IE. Internet has to die!
You might also want to look at visibility: collapse. I don't know the
level of browser support for that either, but in that case you'd be
using:

    visibility = (checkbox.checked) ? "visible" : "collapse";
    document.getElementById(id).style.visibility = visibility;

IE 6 and IE 7 has no idea of the value 'collapse' for the visibility
property.
And you'd also want to set it on the tr. In any case you have to get rid
of the div unless you put in either inside a table-cell or completely
outside the table.

Yes, get rid of the divs. Her is my suggestion:

<table>
<tr>
<td></td>
<td><input name="car" value="T" type="checkbox"
onclick="someFunction(this)">Car</td>
</tr>
<tr id="make">
<td>Make:</td>
<td><input id="made" size="20"></td>
</tr>
<tr id="model">
<td>Model:</td>
<td><input id="model" size="20"></td>
</tr>
</table>

....

<script type="text/javascript">

var oldonload = window.onload;

if (typeof oldonload !== 'function') {
window.onload = someOnloadFunction;
} else {
window.onload = function() {
oldonload();
someOnloadFunction();
}
}

function someOnloadFunction() {

document.getElementById("make").style.display = "none";
document.getElementById("model").style.display = "none";

}

function someFunction( checkbox ) {
display = ( checkbox.checked ) ? "" : "none";
document.getElementById("make").style.display = display;
document.getElementById("model").style.display = display;
}

</script>

You might want do the onload part simpler, like:

<body onload="someOnloadFunction()">

but that could override other onload functions in external scripts.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top