473,378 Members | 1,395 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,378 software developers and data experts.

Problem Changing Options in <SELECT>

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>

Jul 20 '05 #1
7 2245
Lee
Hal Vaughan said:
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,


Don't use document.write() in a page that's already loaded.
As soon as you write() new content to the page, the previous
contents, including your form, the select object and the
options are all deleted.

Use alert() popups or open a second page and write to it.

Jul 20 '05 #2
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:94pBb.481320$HS4.3711221@attbi_s01...
<snip>
..., I have to call the script from within the <FORM>,
What do you mean by "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.) ... <snip>

Mostly accessing a form as a property of document should work. I prefer
the more formal approach of accessing the form as a named property of
the document.forms collection and the form elements as named properties
of the form's elements collection:-

document.forms.oForm.elements.selectLetterNames

-or:-

document.forms['oForm'].elements['selectLetterNames']
function setSelect(oSelect, value) {
//function setSelect() {
var otest = document.oForm.selectLetterName;
document.write("Starting with select<br>");
Once a document is closed (which happens somewhere between the browser
reading the end of the HTML and it triggering the onload event) any
document.write (or writeln) call will open, clear and replace the
current document. so following the preceding statement the object that
the local variable - otest - was pointing to will have ceased to exist.
Any subsequent attempt to access it is likely to produce errors.
var newOption = new Option("The Text", "TheValue");
There are some browsers that do not have an Option constructor (Web
Browser 2.0 on the palm OS at least) so the general principal of testing
browser features prior to using them should certainly be applied to
scripts that attempt to create new Options.

<snip>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.)
JavaScript is probably best though of as a language for scripting object
models. In the case of web browsers that object model is the browser's
document object model (DOM). When it comes to whether a script will work
on a browser the answer is more often than not to be found in the
browser's DOM implementation.

<snip> <SCRIPT LANGUAGE=JavaScript>

<snip>

The language attribute is deprecated in HTML 4 and the type attribute is
required, so maybe - <script type="text/javascript"> - instead.

Richard.
Jul 20 '05 #3
Richard Cornford wrote:
"Hal Vaughan" <ha*@thresholddigital.com> wrote in message
news:94pBb.481320$HS4.3711221@attbi_s01...
<snip>
..., I have to call the script from within the <FORM>,
What do you mean by "call the script from within the <FORM>"?


If I have the script tags, SRC= for scripts in files, and the rest of the
script within the <FORM></FORM> tags, it's aware of the object in the form,
otherwise it isn't.
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.) ...

<snip>

Mostly accessing a form as a property of document should work. I prefer
the more formal approach of accessing the form as a named property of
the document.forms collection and the form elements as named properties
of the form's elements collection:-

document.forms.oForm.elements.selectLetterNames

-or:-

document.forms['oForm'].elements['selectLetterNames']


I didn't know about that. I've done very little programming in JavaScript.
(I used to hate it and think it was screwed up, but then I found I had
learned it from a bad text, and that was the cause of most of my
frustrations.)
function setSelect(oSelect, value) {
//function setSelect() {
var otest = document.oForm.selectLetterName;
document.write("Starting with select<br>");


Once a document is closed (which happens somewhere between the browser
reading the end of the HTML and it triggering the onload event) any
document.write (or writeln) call will open, clear and replace the
current document. so following the preceding statement the object that
the local variable - otest - was pointing to will have ceased to exist.
Any subsequent attempt to access it is likely to produce errors.


Didn't know that at all. Thanks for that info.
var newOption = new Option("The Text", "TheValue");


There are some browsers that do not have an Option constructor (Web
Browser 2.0 on the palm OS at least) so the general principal of testing
browser features prior to using them should certainly be applied to
scripts that attempt to create new Options.


Good point, and thanks. This is intended for a limited audience, and I can
easily put a restriction on it and say it doesn't support handhelds.
<snip>
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.)
JavaScript is probably best though of as a language for scripting object
models. In the case of web browsers that object model is the browser's
document object model (DOM). When it comes to whether a script will work
on a browser the answer is more often than not to be found in the
browser's DOM implementation.


Hadn't looked at it that way at all. Again, thanks for some good info.
<snip>
<SCRIPT LANGUAGE=JavaScript> <snip>

The language attribute is deprecated in HTML 4 and the type attribute is
required, so maybe - <script type="text/javascript"> - instead.


So will that work acceptably in older browsers? If not, how far back (on IE
and NN, for example) can I expect it to work?
Richard.

Thanks for a LOT of very useful information. It's been a big help to me!

Hal
Jul 20 '05 #4
Hal Vaughan wrote:
Richard Cornford wrote:
"Hal Vaughan" wrote:
<snip>
..., I have to call the script from within the <FORM>,


What do you mean by "call the script from within the <FORM>"?


If I have the script tags, SRC= for scripts in files, and the rest of the
script within the <FORM></FORM> tags, it's aware of the object in the form,
otherwise it isn't.


Worng. Objects can be referenced in the Document Object Model
(DOM) of the user agent once they have been loaded.

Placing `script' _elements_ within other elements is not only
not necessary but also results in invalid HTML in many cases,
and if the referenced object was not properly loaded, also in
script errors. For forms this means you cannot presume that
the respective HTMLFormElement object can be referenced
successfully before the `load' event of the `body' element occurs,
i.e. its `onload' event handler fires.
PointedEars
Jul 20 '05 #5
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Placing `script' _elements_ within other elements is not only
not necessary but also results in invalid HTML in many cases,


How, other than intertwined (improperly nested) start/end tags?
(This is an honest question.)

--
Chris Jeris cj****@oinvzer.net Apply (1 6 2 4)(3 7) to domain to reply.
Jul 20 '05 #6
Christopher Jeris wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Placing `script' _elements_ within other elements is not only
not necessary but also results in invalid HTML in many cases,
How, other than intertwined (improperly nested) start/end tags?


I have seen people writing

<table ...>
...
<script type="text/javascript">
for (var i = 0; i < data.length; i++)
{
document.write("<tr>...<\/tr>");
}
</script>
...
</table>

for example.
(This is an honest question.)


Of course.
PointedEars
Jul 20 '05 #7
Hi,

Christopher Jeris wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
Placing `script' _elements_ within other elements is not only
not necessary but also results in invalid HTML in many cases,

How, other than intertwined (improperly nested) start/end tags?
(This is an honest question.)


One (IMHO) excellent example is the APPLET tag. Many people ask how it's
possible to set the list of parameters at runtime, with document.write.
The following doesn't work at all (the applet will not be displayed):

<APPLET ...>
<SCRIPT>
document.writeln( '<PARAM...>' );
</SCRIPT>
</APPLET>

(of course replace the '...' with real values).

So the solution to that is either to document.write the full
<APPLET...</APPLET> tag pair, including all the parameters, or to use
LiveConnect at runtime to communicate the parameters to the Applet.

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #8

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

Similar topics

3
by: Cristian Martinello | last post by:
How can I add <OPTION> to a tag <SELECT> runtime ? the code combo.innerHTML+="<OPTION value='1'>Name</OPTION>" doesn't work... -- Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
3
by: Darren | last post by:
Please Help Me!! I've created a typical <form> and a <select> element. The options are created dynamically from my access database (using asp). There are about 70 options, and when you click...
4
by: Bart van Deenen | last post by:
Hi all I have a script where I dynamically create multiple inputs and selects from a script. The inputs and selects must have an associated onchange handler. I have the script working fine on...
1
by: Russell Campbell | last post by:
I have a script that I use with no problems when the Size attribute of the Select tag is set to more than one (I have it set to 5, to be specific). But when I drop it to 1 to create a popup, the...
4
by: Glen K | last post by:
The following works in Firefox buy not in Internet Explorer: <select> <option> option1 </option> <option style="font-style: bold"> option2 </option> </select> Are there any workarounds to...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
7
by: Alex Maghen | last post by:
I have some client-side JavaScript that I want to run whenever a pulldown <SELECT> is changes on th client. I'm trying to do this as follows... <select id="MyPulldown"...
4
by: Man-wai Chang | last post by:
-- iTech Consulting Co., Ltd. Expert of ePOS solutions Website: http://www.itech.com.hk (IE only) Tel: (852)2325 3883 Fax: (852)2325 8288
1
by: thesti | last post by:
hello, i need to validate birthday using javascript. the validation is that for example user couldn't choose the date 29, 30 and 31 if she has chosen February as the month before and the year % 4...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.