Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

show/hide input based on selection 1

Status
Not open for further replies.

ttuser4

MIS
Jun 19, 2008
147
CA
Hi, I have one selection (R4) and want to hide input (color1other) unless selected "other"
so far the input stays on all the the time
any help how to make it work, please?

<style type="text/css">
.hidden {
display: none;
}
</style>

<script type="text/javascript">

function enableOther()
{
if (!document.getElementById) return;
var s = document.getElementById('R4');
if (!s) return;
if (s.options[s.selectedIndex].value != 'yes') return;
var t = document.getElementById('color1other');
if (!t) return;
t.disabled = false;
t.className = '';
}

function init()
{
if (!document.getElementById) return;
var s = document.getElementById('R4');
if (!s) return;
s.onchange = enableOther;
var t = document.getElementById('color1other');
if (!t) return;
t.disabled = true;
t.className = 'hidden';
}

window.onload = init;
</script>
...
<td width="*"><select size="1" id="R4" name="R4" onchange="enableOther(this);">
<option selected value="noSS">Sand Stone (Ral 1019)</option>
<option value="noEW">Euro White (Ral 9010)</option>
<option value="noQG">Quartz Grey (Ral 8014)</option>
<option value="noJB">Java Brown (Ral 8014)</option>
<option value="yes">other</option>
<input type="text" id="color1other" name="color1other" Size=15>
</td>
 
Hi

I would use only this JavaScript ( with the same HTML ) :
Code:
function enableOther(s)
{
  if (!document.getElementById) return;
  var t = document.getElementById('color1other');
  if (!t) return;
  t.disabled = s.options[s.selectedIndex].value != 'yes'
  t.className = t.disabled ? 'hidden' : '';
}

window.onload = function() { enableOther(document.getElementById('R4')) }

Feherke.
 
to feherke - I tried your code but it didn't make any difference

to ggriffit - under what category?
 
used code:

<script type="text/javascript">

function enableOther(s)
{
if (!document.getElementById) return;
var t = document.getElementById('color1other');
if (!t) return;
t.disabled = s.options[s.selectedIndex].value != 'yes'
t.className = t.disabled ? 'hidden' : '';
}

window.onload = function() { enableOther(document.getElementById('R4')) }

</script>
...
<td width="*"><select size="1" name="R4" id="R4" onchange="enableOther(this);">
<option selected value="noSS">Sand Stone (Ral 1019)</option>
<option value="noEW">Euro White (Ral 9010)</option>
<option value="noQG">Quartz Grey (Ral 8014)</option>
<option value="noJB">Java Brown (Ral 8014)</option>
<option value="yes">other</option>
<input type="text" id="color1other" name="color1other" Size=15>
</td>
 
yes, this is what I want, but the problem is that the color1other input is showed/visible all the time, regardless of the selection.
 
Hi

You had a [tt]style[/tt] in your original code. Do you have it ? That is still needed.

Or do it without that [tt]style[/tt] :
Code:
function enableOther(s)
{
  if (!document.getElementById) return;
  var t = document.getElementById('color1other');
  if (!t) return;
  t.disabled = s.options[s.selectedIndex].value != 'yes'
  t.[red]style.display[/red] = t.disabled ? '[red]none[/red]' : '[red]inline[/red]';
}

window.onload = function() { enableOther(document.getElementById('R4')) }


Feherke.
 
yes, I still had that style.

thanks, this your new code works, except the color1other input is there on load (it works when selection changes)
 
not sure, there might be. i don't have all the code. just want modify one form.
 
yes, I found it and added:

<body onload="stripe('info'); stripe('spec'); stripe('meas'); stripe('gable'); stripe('acc'); stripe('extra'); stripe('distax'); stripe('serv'); enableOther(document.getElementById('R4');">

now it works ok. thank you!
 
now the other script, stripe(), doesn't work (onload event)

 
Hi

There is a syntax error :
Code:
<body onload="stripe('info'); stripe('spec'); stripe('meas'); stripe('gable'); stripe('acc'); stripe('extra'); stripe('distax'); stripe('serv'); enableOther(document.getElementById('R4')[COLOR=red pink])[/color];">
I suppose that you kept the [tt]window.onload = function() { enableOther(document.getElementById('R4')) }[/tt] in place. Until now, that was overwritten by the [tt]body[/tt] tag's [tt]onload[/tt] attribute, so never existed anymore when the time of its execution arrived. But now you made a syntax error in the [tt]body[/tt] tag's [tt]onload[/tt] attribute, and the [tt]window.onload[/tt] [tt]function[/tt] was not replaced anymore with the now invalid code.

Hmm... If the above explanation was unclear, just add the missing closing parenthesis ( ) ). (-:

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top