473,320 Members | 1,695 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,320 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 5855
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.