mantrid wrote on 13 nov 2006 in comp.lang.javascript
:
Evertjan.
that work a treat. can i trouble you for an explanation of how the
code is working. ie what is each of the items below doing?
particularly the innerHTML = (mydropdownlist<6)
the '?'
and the ':'
is it the same as an if...else....? im trying to get my head around
it.
Yes, it is called a ternary operator.
It pays off to read the NG FAQ and the Jscript specs:
<http://www.jibbering.com/faq/>
<
http://msdn.microsoft.com/library/en...7399ac32-9324-
4a9a-ae76-be9c0f9df81c.asp>
droparea.innerHTML = (mydropdownlist<6)
? '<input name="ans1" type="text">'
: '<textarea name="ans1" rows="1"></textarea>';
sorry to be a nuisance. at the moment one of the elements will only
display on clicking the dropdown list. what would be the best approach
to have one as the default which is present when the page loads?
==========================================
<script type='text/javascript'>
function change(x){
var mydropdownlist = x.options[x.selectedIndex].value;
var droparea = document.getElementById('droparea');
droparea.innerHTML = (mydropdownlist<6)
? '<input name="ans1" type="text">'
: '<textarea name="ans1" rows="1"></textarea>';
}
</script>
<form>
<select onclick='change(this)'>
<option value='1'1
<option value='2'2
<option value='8'8
</select>
<br><br><br><br><br><br><br><br>
<div id='droparea'>
<input name="ans1" type="text">
</div>
</form>
=============================================
Simple, isn't it??
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)