H
Hal Vaughan
I have a sample script from a book ("Beginning JavaScript" by Paul Wilton)
that removes or adds a choice to a <SELECT> element. The <FORM> is form1
and the <SELECT> is theDay. The example uses these lines (full text is
below):
if (document.form1.theDay.options[2].text != "Wednesday) {
var days = document.form1.theDay;
days.options[indexCounter].text = days.options[indexCounter - 1].text;
<snip>
var option - new Option("Wednesday", 2);
days.options[2] = option;
}
And everything works fine. (I'm testing this on Konquer and Galeon on Linux
and Opera and IE on Windows.)
Now, in my page, I have a <FORM> named oForm and a <SELECT> named
selectLetterNames. I've found that, in order to access the <SELECT>
element, I have to call the script from within the <FORM>, I can't access
it from outside with "document.oForm.selectLetterNames" (Why is this? I
would think if I specify it using proper notation I could access it
anywhere in the document.) I have an array and I'm trying to put the items
into selectLetterNames. I doubt the entire code is necessary, but I've
tested a few different lines. (The commented out lines are ones that I've
used for testing.):
function setSelect(oSelect, value) {
//function setSelect() {
var otest = document.oForm.selectLetterName;
document.write("Starting with select<br>");
var newOption = new Option("The Text", "TheValue");
// otest.options[1].text = "MyNewText";
// document.oForm.selectLetterName.options[1].text = "MyNewText";
// otest.options[0] = newOption;
for (index in value) {
document.write("Index: " + index + ", Value: " + value[index] + "<br>");
// oTest.options[index] = newOption;
// newOption=(value[index], value[index]);
// oSelect.options[index] = newOption;
// document.oForm.selectLetterName.options[index] = newOption;
}
// var oOpt = document.oForm.selectLetterName.options[0];
// if (document.oForm.selectLetterName.options[0].text == "Text") {
// alert('Equal');
// }
// oTest.options[0] = null;
// document.oForm.selectLetterName.options[0].text = "text";
// document.oForm.selectLetterName.options[0].value = "value";
document.write("Done with loop<br>");
var oTest1 = document.oForm;
document.write("Between assigns<br>");
var oTest2 = document.oForm.selectLetterName;
document.write("Done with select<br>");
return;
}
Whenever I do ANYTHING using the options properties, I get errors, for
example, I can't see why this won't work:
var otest = document.oForm.selectLetterName;
var newOption = new Option("The Text", "TheValue");
otest.options[0] = newOption;
But the last line ALWAYS creates an error. I can't understand why the
example above, from the book works, but no matter what I do, whenever I try
to do ANYTHING with the .options[x] on the <SELECT> object, it won't work.
I also can't see why it works in the example from the book, but not in my
routine. There are no errors generated by accessing the <SELECT> object
itself, only any .options[x] elements.
Also -- what version of JavaScript does a browser need for this to work (I'm
not worried about my browsers -- it obviously works in them since the
example from the book works.)
I'm aware it could even be a typo -- I've gone over and over the code
carefully, but sometimes I can't distinguish between similar character
sequences.
Thanks for any help!
Hal
------------------------------------------------------------------------------
<SCRIPT LANGUAGE=JavaScript>
function butRemoveWed_onclick()
{
if (document.form1.theDay.options[2].text == "Wednesday")
{
document.form1.theDay.options[2] = null;
}
else
{
alert('There is no Wednesday here!');
}
}
function butAddWed_onclick()
{
if (document.form1.theDay.options[2].text != "Wednesday")
{
var indexCounter;
var days = document.form1.theDay;
var lastoption = new Option();
days.options[6] = lastoption;
for (indexCounter = 6;indexCounter > 2; indexCounter--)
{
days.options[indexCounter].text = days.options[indexCounter - 1].text;
days.options[indexCounter].value = days.options[indexCounter -
1].value;
}
var option = new Option("Wednesday",2);
days.options[2] = option;
}
else
{
alert('Do you want to have TWO Wednesdays?????');
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=form1>
<SELECT NAME=theDay SIZE=5>
<OPTION VALUE=0 SELECTED>Monday
<OPTION VALUE=1>Tuesday
<OPTION VALUE=2>Wednesday
<OPTION VALUE=3>Thursday
<OPTION VALUE=4>Friday
<OPTION VALUE=5>Saturday
<OPTION VALUE=6>Sunday
</SELECT>
<BR>
<INPUT TYPE="button" VALUE="Remove Wednesday" NAME=butRemoveWed
onclick="butRemoveWed_onclick()">
<INPUT TYPE="button" VALUE="Add Wednesday" NAME=butAddWed
onclick="butAddWed_onclick()">
<BR>
</FORM>
</BODY>
</HTML>
that removes or adds a choice to a <SELECT> element. The <FORM> is form1
and the <SELECT> is theDay. The example uses these lines (full text is
below):
if (document.form1.theDay.options[2].text != "Wednesday) {
var days = document.form1.theDay;
days.options[indexCounter].text = days.options[indexCounter - 1].text;
<snip>
var option - new Option("Wednesday", 2);
days.options[2] = option;
}
And everything works fine. (I'm testing this on Konquer and Galeon on Linux
and Opera and IE on Windows.)
Now, in my page, I have a <FORM> named oForm and a <SELECT> named
selectLetterNames. I've found that, in order to access the <SELECT>
element, I have to call the script from within the <FORM>, I can't access
it from outside with "document.oForm.selectLetterNames" (Why is this? I
would think if I specify it using proper notation I could access it
anywhere in the document.) I have an array and I'm trying to put the items
into selectLetterNames. I doubt the entire code is necessary, but I've
tested a few different lines. (The commented out lines are ones that I've
used for testing.):
function setSelect(oSelect, value) {
//function setSelect() {
var otest = document.oForm.selectLetterName;
document.write("Starting with select<br>");
var newOption = new Option("The Text", "TheValue");
// otest.options[1].text = "MyNewText";
// document.oForm.selectLetterName.options[1].text = "MyNewText";
// otest.options[0] = newOption;
for (index in value) {
document.write("Index: " + index + ", Value: " + value[index] + "<br>");
// oTest.options[index] = newOption;
// newOption=(value[index], value[index]);
// oSelect.options[index] = newOption;
// document.oForm.selectLetterName.options[index] = newOption;
}
// var oOpt = document.oForm.selectLetterName.options[0];
// if (document.oForm.selectLetterName.options[0].text == "Text") {
// alert('Equal');
// }
// oTest.options[0] = null;
// document.oForm.selectLetterName.options[0].text = "text";
// document.oForm.selectLetterName.options[0].value = "value";
document.write("Done with loop<br>");
var oTest1 = document.oForm;
document.write("Between assigns<br>");
var oTest2 = document.oForm.selectLetterName;
document.write("Done with select<br>");
return;
}
Whenever I do ANYTHING using the options properties, I get errors, for
example, I can't see why this won't work:
var otest = document.oForm.selectLetterName;
var newOption = new Option("The Text", "TheValue");
otest.options[0] = newOption;
But the last line ALWAYS creates an error. I can't understand why the
example above, from the book works, but no matter what I do, whenever I try
to do ANYTHING with the .options[x] on the <SELECT> object, it won't work.
I also can't see why it works in the example from the book, but not in my
routine. There are no errors generated by accessing the <SELECT> object
itself, only any .options[x] elements.
Also -- what version of JavaScript does a browser need for this to work (I'm
not worried about my browsers -- it obviously works in them since the
example from the book works.)
I'm aware it could even be a typo -- I've gone over and over the code
carefully, but sometimes I can't distinguish between similar character
sequences.
Thanks for any help!
Hal
------------------------------------------------------------------------------
<SCRIPT LANGUAGE=JavaScript>
function butRemoveWed_onclick()
{
if (document.form1.theDay.options[2].text == "Wednesday")
{
document.form1.theDay.options[2] = null;
}
else
{
alert('There is no Wednesday here!');
}
}
function butAddWed_onclick()
{
if (document.form1.theDay.options[2].text != "Wednesday")
{
var indexCounter;
var days = document.form1.theDay;
var lastoption = new Option();
days.options[6] = lastoption;
for (indexCounter = 6;indexCounter > 2; indexCounter--)
{
days.options[indexCounter].text = days.options[indexCounter - 1].text;
days.options[indexCounter].value = days.options[indexCounter -
1].value;
}
var option = new Option("Wednesday",2);
days.options[2] = option;
}
else
{
alert('Do you want to have TWO Wednesdays?????');
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=form1>
<SELECT NAME=theDay SIZE=5>
<OPTION VALUE=0 SELECTED>Monday
<OPTION VALUE=1>Tuesday
<OPTION VALUE=2>Wednesday
<OPTION VALUE=3>Thursday
<OPTION VALUE=4>Friday
<OPTION VALUE=5>Saturday
<OPTION VALUE=6>Sunday
</SELECT>
<BR>
<INPUT TYPE="button" VALUE="Remove Wednesday" NAME=butRemoveWed
onclick="butRemoveWed_onclick()">
<INPUT TYPE="button" VALUE="Add Wednesday" NAME=butAddWed
onclick="butAddWed_onclick()">
<BR>
</FORM>
</BODY>
</HTML>