...

View Full Version : FireFox and Select Element Handling Question



greasonwolfe
02-26-2006, 11:12 PM
Can someone please tell me why this script works in IE but fails in FF? I've tried to research the problem but have as yet to find anything truly discussing the way FF handles the select box element. Or, if nobody can explain it concisely, can someone at least point me in the right direction to something that will explain it?



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">

<html lang=en-us>

<head>

<script type="text/javascript">
<!--

function changevalue()
{
var randomvalue=Math.floor(Math.random()*260)+1
if (randomvalue>=1 && randomvalue<=10) {document.someform.choice.options.value="A"}
if (randomvalue>=11 && randomvalue<=20) {document.someform.choice.options.value="B"}
if (randomvalue>=21 && randomvalue<=30) {document.someform.choice.options.value="C"}
if (randomvalue>=31 && randomvalue<=40) {document.someform.choice.options.value="D"}
if (randomvalue>=41 && randomvalue<=50) {document.someform.choice.options.value="E"}
if (randomvalue>=51 && randomvalue<=60) {document.someform.choice.options.value="F"}
if (randomvalue>=61 && randomvalue<=70) {document.someform.choice.options.value="G"}
if (randomvalue>=71 && randomvalue<=80) {document.someform.choice.options.value="H"}
if (randomvalue>=81 && randomvalue<=90) {document.someform.choice.options.value="I"}
if (randomvalue>=91 && randomvalue<=100) {document.someform.choice.options.value="J"}
if (randomvalue>=101 && randomvalue<=110) {document.someform.choice.options.value="K"}
if (randomvalue>=111 && randomvalue<=120) {document.someform.choice.options.value="L"}
if (randomvalue>=121 && randomvalue<=130) {document.someform.choice.options.value="M"}
if (randomvalue>=131 && randomvalue<=140) {document.someform.choice.options.value="N"}
if (randomvalue>=141 && randomvalue<=150) {document.someform.choice.options.value="O"}
if (randomvalue>=151 && randomvalue<=160) {document.someform.choice.options.value="P"}
if (randomvalue>=161 && randomvalue<=170) {document.someform.choice.options.value="Q"}
if (randomvalue>=171 && randomvalue<=180) {document.someform.choice.options.value="R"}
if (randomvalue>=181 && randomvalue<=190) {document.someform.choice.options.value="S"}
if (randomvalue>=191 && randomvalue<=200) {document.someform.choice.options.value="T"}
if (randomvalue>=201 && randomvalue<=210) {document.someform.choice.options.value="U"}
if (randomvalue>=211 && randomvalue<=220) {document.someform.choice.options.value="V"}
if (randomvalue>=221 && randomvalue<=230) {document.someform.choice.options.value="W"}
if (randomvalue>=231 && randomvalue<=240) {document.someform.choice.options.value="X"}
if (randomvalue>=241 && randomvalue<=250) {document.someform.choice.options.value="Y"}
if (randomvalue>=251 && randomvalue<=260) {document.someform.choice.options.value="Z"}
}

-->
</script>

</head>

<body>

<form name="someform">
<select name="choice">
<option value="None"></option>
<option value="A">Value of A</option>
<option value="B">Value of B</option>
<option value="C">Value of C</option>
<option value="D">Value of D</option>
<option value="E">Value of E</option>
<option value="F">Value of F</option>
<option value="G">Value of G</option>
<option value="H">Value of H</option>
<option value="I">Value of I</option>
<option value="J">Value of J</option>
<option value="K">Value of K</option>
<option value="L">Value of L</option>
<option value="M">Value of M</option>
<option value="N">Value of N</option>
<option value="O">Value of O</option>
<option value="P">Value of P</option>
<option value="Q">Value of Q</option>
<option value="R">Value of R</option>
<option value="S">Value of S</option>
<option value="T">Value of T</option>
<option value="U">Value of U</option>
<option value="V">Value of V</option>
<option value="W">Value of W</option>
<option value="X">Value of X</option>
<option value="Y">Value of Y</option>
<option value="Z">Value of Z</option>
</select>
<br>
<input type="button" value="Random Selection" onClick="changevalue()">
</form>

</body>

</html>


GW

fishluvr
02-27-2006, 07:05 PM
Try setting the selectedIndex vice the value.

Beagle
02-27-2006, 07:24 PM
You need to set the value of the select box itself, not it's options collection.

So you need

document.someForm.choice.value = "A";

Also, realize that each of those conditionals is being evaluated. You should "else if" them all so that you don't end up doing more processing than you have to.

greasonwolfe
02-27-2006, 11:13 PM
Thanks Beagle. The second time you answered (another thread) was actually an older thread, but hey, I got the message. LOL. Will fiddle with it and see what I come up with. For some reason, though, I got the idea that the options value had to be set. At least that was the way I interpreted what I read at Javascriptkit. But then again, I might have been tired when I read it.

GW

Beagle
02-28-2006, 05:29 PM
The options attribute of a select box is an array that contains all of the options in the select box. If you try to set the value of the options attribute to a scalar, it simply doesn't make any sense.

Pseudo-Example:

select.options = {'a':'Letter A','b':'Letter B'};

Results in a select box with two options, "Letter A" and "Letter B".

The select box takes that array and at some level loops over the elements in the array populates the box.

How would you transfer that behavior to a scalar value, as you are attempting?

Can you loop over the value: 'a' and generate the drop down box's options? It just doesn't make sense.

To further your understanding, try to get the value of the select box, instead of setting it. Do an onchange="alert(this.value)" versus an onchange="alert(this.options.value)" You should start getting a clearer idea of what's going on.

greasonwolfe
02-28-2006, 07:21 PM
Mmmm. . how to explain this in full.

The easiest way to explain it would be to say that the select box is serving several purposes at once.

1) The user can select a value

or

2) The value can be determined randomly by the script

additionally,

3) The value represents a "cross reference" to other data I am working with (e.g. the assigned value for each option has a specific meaning in relation to the rest of the code)

4) I needed a way to access the selectedIndex value so that it could be used in conjunction with another array to help assign image and display values of processed data.

The ABC.... example was a trimmed down and simpler version of the function I was actually working on at the time and was meant to serve the purpose of figuring out what I was doing wrong in trying to reference it. The actual function I am working with generates a weighted random value (assuming the user doesnt pick a value) based on the probability of a particular condition (in this case, a star's particular spectral class) occuring.

In any event, with your help, I have the script working the way it is supposed to and while it may not make complete sense to everyone, it makes sense to me and my whacked out mind. LOL.

Again, thanks for the help.

GW



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum