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

drop down error

hello,
Question, on page load, I populate an existing drop down with
createElement and appendChild. It works fine so far. BUT I want to
automatically select some option from this populated drop down. So i
have this statement:

document.getElementById("dropdown").options[someindex].selected=true;

And it gives me an error.

Surprisingly, when i put an alert statement before appending the
created element to dropdown, the error doesn't occur.
Can anyone help please?

Thanks

Jul 23 '05 #1
7 5857
debugger wrote:
hello,
Question, on page load, I populate an existing drop down with
createElement and appendChild. It works fine so far. BUT I want to
automatically select some option from this populated drop down. So i
have this statement:

document.getElementById("dropdown").options[someindex].selected=true;

And it gives me an error.
What is the error message?
Surprisingly, when i put an alert statement before appending the
created element to dropdown, the error doesn't occur.
That indicates a timing issue.
Can anyone help please?


Start with the group FAQ

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
debugger wrote:
hello,
Question, on page load, I populate an existing drop down with
createElement and appendChild. It works fine so far. BUT I want to
automatically select some option from this populated drop down. So i
have this statement:

document.getElementById("dropdown").options[someindex].selected=true;

And it gives me an error.

Surprisingly, when i put an alert statement before appending the
created element to dropdown, the error doesn't occur.
Can anyone help please?


Probably the browser is still creating the option when you try
to grab it with getElementById. Putting in the alert gives the
browser time to create it.

Why not set the selected attribute when you create the option?

...
var oOpt = document.createElement('option');
oOpt.selected = true;
...

Then you don't need to use gEBI.
--
Rob
Jul 23 '05 #3
In article <11**********************@c13g2000cwb.googlegroups .com>,
js********@yahoo.com enlightened us with...
Question, on page load, I populate an existing drop down with
createElement and appendChild. It works fine so far. BUT I want to
automatically select some option from this populated drop down. So i
have this statement:

document.getElementById("dropdown").options[someindex].selected=true;

And it gives me an error.
Magic 8 ball failed.
What's the error?

Surprisingly, when i put an alert statement before appending the
created element to dropdown, the error doesn't occur.
Can anyone help please?


Not without seeing actual code, no.
But more than likely, you're trying to access something that hasn't been
fully created/rendered yet.

--
--
~kaeli~
A little rudeness and disrespect can elevate a meaningless
interaction to a battle of wills and add drama to an
otherwise dull day.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #4
the error is "htmlfile: Could not set the selected property.
Unspecified error."

option element for drop down is created by:
for(.......)
{
var myEle = document.createElement("option");
myEle.value=some value;
myEle.innerHTML= some string;
document.getElementById("someDropDown").appendChil d(myEle);
}
after this, there is other for loop setting the selected property.
code causing the error:
for(var k=0; k<something; k++)
{
if(somecodition)
{
var mySelectedOption =
document.getElementById("someDropDown").options[k];
mySelectedOption.selected=true; <<<<<<<<<<<<======== error
occurs here
}
}

now when i put alert in the above "if statement", the error does not
occur.
Folks have already pointed out its a timing issue, thanks for that. But
I m not sure how to solve it.
thanks again.

Jul 23 '05 #5
debugger wrote:
the error is "htmlfile: Could not set the selected property.
Unspecified error."

option element for drop down is created by:
for(.......)
{
var myEle = document.createElement("option");
myEle.value=some value;
myEle.innerHTML= some string;
Yuck. Why use innerHTML?

myEle.appendChild(document.createTextNode('some string'));
document.getElementById("someDropDown").appendChil d(myEle);
Do some feature detection before using gEBI (and you should
probably include a document.all method too if using gEBI):

if (document.getElementById) {
document.getElementById("someDropDown")...;
...

Or even better, use the forms collection:

document.forms['someDropDown']...;

Avoids gEBI entirely - but see notes below.
}
after this, there is other for loop setting the selected property.
code causing the error:
for(var k=0; k<something; k++)
{
if(somecodition)
{
var mySelectedOption =
document.getElementById("someDropDown").options[k];
mySelectedOption.selected=true; <<<<<<<<<<<<======== error
occurs here
Why not keep a reference to "someDropDown" when you access it
above, then use it again when referencing the option:

var myDropDown = document.forms['someDropDown'];
myDropDown.appendChild(myEle);

...

myDropDown.options[k].selected = true;
Now there is no need for the second gEBI (or if the forms
collection is used, no need for gEBI at all)

[...] now when i put alert in the above "if statement", the error does not
occur.


Whilst Firefox does not generate an error, IE does. The error
is not fatal and the script appears to execute properly - the
option is added and selected. If users have not set IE to
report JavaScript errors, they will not see any error and the
script works fine. I would call this a bug in IE.

You can use setAttribute to keep IE happy, but then Firefox will
not select the option. Given that browser detection should only
be used as an absolute last resort, this would seem an
appropriate place to use try/catch:

if ( i == len-1){
try {
mySel.options[i].selected = true;
} catch(e) {
mySel.options[i].setAttribute('selected', true);
}
}

In future, please post code that actually replicates the error,
it makes the job of suggesting fixes much easier.

The following code implements the suggestions above and fixes
the error.

I still don't understand why you don't set myEle (the option) to
selected when you create it, that way works in both Firefox and
IE without the try/catch nastiness.
<html><head><title>play</title>
</head><body>
<script type="text/javascript">
function doStuff() {
var mySel = document.forms['aForm'].elements['someDropDown'];
var myEle = document.createElement('option');
myEle.value= 'someValue';
myEle.appendChild(document.createTextNode('some string'));

// why not set it to selected here?
// myEle.selected = true;

mySel.appendChild(myEle);

// if set to selected above, this whole block is not needed.
var len = mySel.length;
for (var i=0; i<len; i++) {
if ( i == len-1){
try {
mySel.options[i].selected = true;
} catch(e) {
mySel.options[i].setAttribute('selected', true);
}
}
}

}

</script>
</head><body>

<form action="" name="aForm">
<select name="someDropDown" id="someDropDown">
<option value="steve">Steve</option>
<option value="harry">Harry</option>
<option value="sue">Sue</option>
</select>
<br>
<input type="button" onclick="doStuff();" value="Click">
</form>

</body></html>

--
Rob
Jul 23 '05 #6
Thank alot Rob,

Your try and catch did the job. I didn't know about the
document.createTextNode() earlier. And thanks for pointing that out.
For your question:
// why not set it to selected here?
// myEle.selected = true;
I have three for loops and in the third for loop, i have an if
conditional to check for which item to select(as there are many to
select from). Now for others to understand my code, I have the "if"
conditional in separate for loop. Therefore I did that just for
readability purposes. I think are right, I should put it after element
is created.

Thanks again,
JS
RobG wrote: debugger wrote:
the error is "htmlfile: Could not set the selected property.
Unspecified error."

option element for drop down is created by:
for(.......)
{
var myEle = document.createElement("option");
myEle.value=some value;
myEle.innerHTML= some string;


Yuck. Why use innerHTML?

myEle.appendChild(document.createTextNode('some string'));
document.getElementById("someDropDown").appendChil d(myEle);


Do some feature detection before using gEBI (and you should
probably include a document.all method too if using gEBI):

if (document.getElementById) {
document.getElementById("someDropDown")...;
...

Or even better, use the forms collection:

document.forms['someDropDown']...;

Avoids gEBI entirely - but see notes below.
}
after this, there is other for loop setting the selected property.
code causing the error:
for(var k=0; k<something; k++)
{
if(somecodition)
{
var mySelectedOption =
document.getElementById("someDropDown").options[k];
mySelectedOption.selected=true; <<<<<<<<<<<<======== error
occurs here


Why not keep a reference to "someDropDown" when you access it
above, then use it again when referencing the option:

var myDropDown = document.forms['someDropDown'];
myDropDown.appendChild(myEle);

...

myDropDown.options[k].selected = true;
Now there is no need for the second gEBI (or if the forms
collection is used, no need for gEBI at all)

[...]
now when i put alert in the above "if statement", the error does not occur.


Whilst Firefox does not generate an error, IE does. The error
is not fatal and the script appears to execute properly - the
option is added and selected. If users have not set IE to
report JavaScript errors, they will not see any error and the
script works fine. I would call this a bug in IE.

You can use setAttribute to keep IE happy, but then Firefox will
not select the option. Given that browser detection should only
be used as an absolute last resort, this would seem an
appropriate place to use try/catch:

if ( i == len-1){
try {
mySel.options[i].selected = true;
} catch(e) {
mySel.options[i].setAttribute('selected', true);
}
}

In future, please post code that actually replicates the error,
it makes the job of suggesting fixes much easier.

The following code implements the suggestions above and fixes
the error.

I still don't understand why you don't set myEle (the option) to
selected when you create it, that way works in both Firefox and
IE without the try/catch nastiness.
<html><head><title>play</title>
</head><body>
<script type="text/javascript">
function doStuff() {
var mySel = document.forms['aForm'].elements['someDropDown'];
var myEle = document.createElement('option');
myEle.value= 'someValue';
myEle.appendChild(document.createTextNode('some string'));

// why not set it to selected here?
// myEle.selected = true;

mySel.appendChild(myEle);

// if set to selected above, this whole block is not needed.
var len = mySel.length;
for (var i=0; i<len; i++) {
if ( i == len-1){
try {
mySel.options[i].selected = true;
} catch(e) {
mySel.options[i].setAttribute('selected', true);
}
}
}

}

</script>
</head><body>

<form action="" name="aForm">
<select name="someDropDown" id="someDropDown">
<option value="steve">Steve</option>
<option value="harry">Harry</option>
<option value="sue">Sue</option>
</select>
<br>
<input type="button" onclick="doStuff();" value="Click">
</form>

</body></html>

--
Rob


Jul 23 '05 #7
debugger wrote:
Thank alot Rob,

Your try and catch did the job. I didn't know about the
document.createTextNode() earlier. And thanks for pointing that out.
For your question:
// why not set it to selected here?
// myEle.selected = true;

I have three for loops and in the third for loop, i have an if
conditional to check for which item to select(as there are many to
select from). Now for others to understand my code, I have the "if"
conditional in separate for loop. Therefore I did that just for
readability purposes. I think are right, I should put it after element
is created.

Thanks again,


Glad to help. :-)

--
Rob
Jul 23 '05 #8

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

Similar topics

4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
1
by: Dan | last post by:
This is one that has me stumped and I need an expert's input. Any ideas why the values from the second script-generated drop down list isn't recognized by the script to add time values to the...
4
by: rajat | last post by:
Hi, I have adapted a drop down menu from USC webpage (www.usc.edu). The link to my page is http://www-scf.usc.edu/~swarup/test/test.html The links to the CSS ans JS files are:...
3
by: Miguel Dias Moura | last post by:
Hello, i have an ASP.NET / VB page where i have a few 4 groups of Drop Down Lists. Each group of Drop Down Lists include 3 Drop Down Lists for date such as: DAY, MONTH, and YEAR. I don't want...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
6
by: Joey Liang via DotNetMonster.com | last post by:
Hi all, I have a drop down list which store all the different brands of product.When i selected the particular brand from the drop down list, it will display all the products with the selected...
6
by: John | last post by:
I have a drop down on my form and I need all the values in that drop down and pass to a stored procedure, how can I get the values of the drop down and pass them all to my stored procedure call?
6
by: zacks | last post by:
I have an application I am developing that has a Combo Box that is intended to show a list of available tables in the selected DSN. I have put code in the control's DropDown event handler to clear...
0
by: yosri2005 | last post by:
Hello, I'm sure many of you have seen the error message in the subject. I found quiet a few posts on the web regarding this issue, but the ones I saw mainly tackle this issue when you have a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.