473,418 Members | 2,138 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,418 software developers and data experts.

javascript to determine which form element is displayed

Hello

Im trying to find the correct syntax to use to determine whether which form
element is visible on changing the value in a dropdown list
I need something in the onChange of the dropdown list that will cause the
appropriate element on another part of the page to be displayed

pseudo code below

var mydropdownlist =
document.questform.typeid.options[document.questform.typeid.selectedIndex].v
alue;
if(mydropdownlist<6)
then display <input name="ans1" type="text" >
else
display <textarea name="ans1" rows="1" ></textarea>

any help appreciated
ian

Nov 12 '06 #1
5 3994
mantrid wrote on 12 nov 2006 in comp.lang.javascript:
Hello

Im trying to find the correct syntax to use to determine whether which
form element is visible on changing the value in a dropdown list
I need something in the onChange of the dropdown list that will cause
the appropriate element on another part of the page to be displayed

pseudo code below

var mydropdownlist =
document.questform.typeid.options[document.questform.typeid.selectedInd
ex].v alue;
if(mydropdownlist<6)
then display <input name="ans1" type="text" >
else
display <textarea name="ans1" rows="1" ></textarea>

<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'></div>
</form>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Nov 12 '06 #2

"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
mantrid wrote on 12 nov 2006 in comp.lang.javascript:
Hello

Im trying to find the correct syntax to use to determine whether which
form element is visible on changing the value in a dropdown list
I need something in the onChange of the dropdown list that will cause
the appropriate element on another part of the page to be displayed

pseudo code below

var mydropdownlist =
document.questform.typeid.options[document.questform.typeid.selectedInd
ex].v alue;
if(mydropdownlist<6)
then display <input name="ans1" type="text" >
else
display <textarea name="ans1" rows="1" ></textarea>


<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'></div>
</form>

thanks Evertjan
that look like what i need
Nov 13 '06 #3

"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
mantrid wrote on 12 nov 2006 in comp.lang.javascript:
Hello

Im trying to find the correct syntax to use to determine whether which
form element is visible on changing the value in a dropdown list
I need something in the onChange of the dropdown list that will cause
the appropriate element on another part of the page to be displayed

pseudo code below

var mydropdownlist =
document.questform.typeid.options[document.questform.typeid.selectedInd
ex].v alue;
if(mydropdownlist<6)
then display <input name="ans1" type="text" >
else
display <textarea name="ans1" rows="1" ></textarea>


<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'></div>
</form>

--
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.
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?

thanks again
ian
Nov 13 '06 #4
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)
Nov 13 '06 #5
>
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>
thanks,will take a look at it. trouble with me is i havnt used javascript
much and i didnt really learn the proper way. i should have really looked at
the basics but i find myself just concentrating on finding solutions for
what i need at the moment. the wrong way around i know

==========================================
<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??
it is. infact that occurd to me to, straight away. but i ruled it out almost
as quickly because i thought that
<div id='droparea'>
<input name="ans1" type="text">

would generate two elements when the dropdown list was clicked instead of
just the one. can understand why it doesnt

ian

Nov 13 '06 #6

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

Similar topics

19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
14
by: horos | last post by:
hey all, I'm a heavy perl user, not so much a java script user, and was wondering... perl has an extremely nice utility called Data::Dumper, which allows you to dump out the contents of an...
11
by: Dag Sunde | last post by:
We have this web-app that requires Javascript to be enabled in the UA (this is okay'ed by our customer). Now I want to make a "pre" page before the login screen, informing the user that he/she...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
7
by: amerar | last post by:
Hi All, I am really green at Javascript. Someone gave me some code to play with and I'm trying to understand it: <INPUT TYPE=text NAME=subject SIZE=60 MAXLENGTH=60><BR><BR> <input...
9
by: Phil_Harvey | last post by:
I am redoing my website and trying to get it to do something more exciting using Javascript. I did normal Java at university and code at work in VB.NET. I have got reasonably far into what I want...
4
by: Adam Smith | last post by:
Hello, How can I call or trigger an external javascript twice in a form? I have <script language="JavaScript" src="country_state.js" name="Country_State"> <script type="text/javascript"...
69
by: Peter Olcott | last post by:
Does JavaScript represent its controls internally as Microsoft Windows controls, or does it build them from scratch like Java?
5
by: rmurgia | last post by:
I set up an Access database with a text field to record the website name and a date field to record the date/time of the access. I have set up a page with various anchors set up to call up different...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.