...

View Full Version : can u solve my problem in javascript?



phani
08-07-2002, 12:07 PM
sir, i have a problem in javascript. my problem looks like this
in the html page assume inputbox,textbox etc in the place of words:
(mc-multiplechoice,fg-fill in the gaps)

Topic Name: inputbox1(values:c,c++,vb)

Question Type: inputbox2(values:mc,fg)

Question: textarea

Answer : textarea

choice1 : textbox1

choice2 : textbox2

choice3 : textbox3

choice4 : textbox4

my problem is i should be able to display only the "Answer" field if i select "fg" from inputbox2 and choices 1 through 4 should be displayed when "mc" is selected(i.e Answer field should not be displayed).can u clarify me this in javascript?

beetle
08-07-2002, 03:38 PM
Are you familiar with the display style property? That, along with some events will get done what you need.

lifer
08-07-2002, 03:51 PM
in more detail, you need to
1) add to the select tag so that it looks like
<SELECT OnChange="display_correct(this);"> ... options.. </SELECT>

2) add some script like this
<SCRIPT language="javascript"><!--
function displaycorrect(obj)
{
if (obj.value=="mc")
{
textareaname.style.display="none";
textbox1name.display="inline";
textbox2name.display="inline";
....
}
else
{
textareaname.style.display="inline";
textbox1name.display="none";
....
}
}

adios
08-08-2002, 02:04 AM
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function getElement(id) {
return document.getElementById ? document.getElementById(id) :
document.all ? document.all(id) : null;
}

function toggle_display(val) {
var el = getElement('txt_answer');
if (el && el.style) el.style.display = (val == 'fg') ? '' : 'none';
var el = getElement('txt_choose');
if (el && el.style) el.style.display = (val == 'mc') ? '' : 'none';
}

</script>
</head>
<body>
<form>
<select name="inputbox1">
<option selected="selected">Topic Name</option>
<option value="c">c</option>
<option value="c++">c++</option>
<option value="vb">vb</option>
</select>
<br>
<select name="inputbox2"
onchange="toggle_display(this[this.selectedIndex].value)">
<option selected="selected">Question Type</option>
<option value="mc">multiple choice</option>
<option value="fg">fill in the gaps</option>
</select><br>
Question: <br>
<textarea name="question" rows="8" cols="64" wrap="virtual"></textarea>
<div id="txt_answer" style="display:none;">
Answer: <br>
<textarea name="answer" rows="8" cols="64" wrap="virtual"></textarea>
</div>
<div id="txt_choose" style="display:none;">
<input type="text" name="choice1" value="choice1">
<input type="text" name="choice2" value="choice2">
<input type="text" name="choice3" value="choice3">
<input type="text" name="choice4" value="choice4">
</div>
<br><input type="submit" value="DONE">
</form>
</body>
</html>

phani
08-08-2002, 08:42 AM
thank u sir, i have got it. can u please explain what this statement function is doing?

return document.getElementById ? document.getElementById(id) :
document.all ? document.all(id) : null;

Is there any property by name "getElementById" in document?
and what document.all meant for?

glenngv
08-08-2002, 09:51 AM
adios used the shortcut for if else statement, which i think is called ternary operator.

variable = (condition) ? valueIfTrue:valueIfFalse;

in the case of his code, he tests if the browser is IE5+ or IE6+ which support document.getElementById, if not he tests if the browser is IE4 which support document.all and if not null is returned.





Originally posted by phani
thank u sir, i have got it. can u please explain what this statement function is doing?

return document.getElementById ? document.getElementById(id) :
document.all ? document.all(id) : null;

Is there any property by name "getElementById" in document?
and what document.all meant for?

phani
08-08-2002, 11:05 AM
thank u sir, for all those who helped me. can u send me the website addresses which provide the indepth knowledge in javascript

lifer
08-08-2002, 11:06 AM
You could try:

http://developer.netscape.com/docs/manuals/communicator/jsref/index.htm

:)

Have fun....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum