PDA

View Full Version : Displaying <div>


ccm3k
12-31-2001, 02:37 AM
Hi

I need help with the following :

I have a form with a master SELECT element. I want a specific DIV to appear when the user selects an item in the list.

Maybe the code below - which is not working and I do not see why (and it is my problem) - will make it clearer :

(knowing that the select has "onchange=showdiv();"

//begin
var nowdisplayed = 0; // the present displayed div
function showdiv() {
var divselected = documents.forms[0].listtype.value;
var seldiv = eval("searchform-"+divselected)
var hidediv = eval("searchform-"+nowdisplayed)
seldiv.style.visibility = "visible";
hidediv.style.visibility = "hidden";
}
//end

The returned error is "style is Null, etc..."

I do not see what is wrong !

Help please !

Thanks

CCM

COBOLdinosaur
12-31-2001, 09:17 PM
Select does not have a value. It has options which have values:
so...
The value is in documents.forms[0].listtype.options[document.forms[0].listtype.selectedIndex].value;

Though I don't know what you would not just do:

onChange="showdiv(this.options[this.selectedIndex].value)"

and just pass the value as an argument to the function.

Jon Hanlon
01-01-2002, 08:04 PM
There is no 'documents' object; there is a 'document' object.
eval("something-" + string) is invalid Javascript (unless 'something' and 'string' are both numbers and you want to do a subtraction.), that is, a minus-sign '-' is not allowed inside a variable. Use an underscore '_'.

[code]

<select onchange="showdiv(this.options[this.selectedIndex].value)">
<option value="div1">Blue Division
<option value="div2">Grey Division
</select>

<script>
var nowdisplayed = null; // the present displayed div
function showdiv(val) {
var seldiv = eval("document.all." + val)
seldiv.style.visibility = "visible";
if (nowdisplayed)
nowdisplayed.style.visibility = "hidden";
nowdisplayed = seldiv;
}

ccm3k
01-02-2002, 05:31 AM
I did not know "-" sign was not allowed in variable names.
I also thought that the value of the selected option in a SELECT element was assimilated to the value of the SELECT element.
- I was wrong !

Thanks for your help !

CCM

PS. "documents" was just a typing mistake...

_mrkite
01-03-2002, 08:59 PM
Just my 2¢, for what it's worth...

Select objects certainly do have a .value property - and it contains the selected value in 90%+ of all browsers (MSIE); Netscape exposes it as well, but leaves it null (presumably inherited from the class). Not sure on NS6.

HNY, all::::cool: