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

"Undefined" in scrolling_list

I tried to update a scrolling_list with the following:

function saveText( scroll_list, t_area, listToBeUpdated ) {
var updated = new Option();
updated.value = t_area.value;
updated.text = t_area.text;
scroll_list.options[scroll_list.selectedIndex] = updated;
}

but I get "Undefined" in the position with the new Option()
(i.e. "updated").

Why and how to fix this?

Jan 20 '06 #1
3 2589
William wrote:
I tried to update a scrolling_list with the following:

function saveText( scroll_list, t_area, listToBeUpdated ) {
var updated = new Option();
updated.value = t_area.value;
updated.text = t_area.text;
scroll_list.options[scroll_list.selectedIndex] = updated;
}

but I get "Undefined" in the position with the new Option()
(i.e. "updated").

Why and how to fix this?
Your code is incomplete, which makes it hard to narrow down the
problem. I'm assuming a "scrolling_list" means a select-tag with size1.


How should your "update" action behave ? First empty the box or just
add new text/value pairs ? I'm assuming the first, otherwise just
comment the while loop in the code.

------------------------

<html>
<head>
<script language="javascript">
function saveText() {
// empty box
while (document.forms[0].scroll_list.options.length)
document.forms[0].scroll_list.options[0] = null;

// put new content in the box
document.forms[0].scroll_list.options[document.forms[0].scroll_list.length]

= new Option("brown", "6");
document.forms[0].scroll_list.options[document.forms[0].scroll_list.length]

= new Option("black", "7");
}
</script>
</head>

<body>
<form>
<select size="3" name="scroll_list">
<option value="1">white</option>
<option value="2">blue</option>
<option value="3">green</option>
<option value="4">red</option>
<option value="5">yellow</option>
</select>
<input type="button" value="Update" onClick="saveText();">
</form>
</body>
</html>

--
Bart

Jan 21 '06 #2
William wrote:
I tried to update a scrolling_list with the following:

function saveText( scroll_list, t_area, listToBeUpdated ) {
var updated = new Option();
updated.value = t_area.value;
updated.text = t_area.text;
Why create a new option when you just want to modify the attributes of
the one that is there?

var o = scrollList.options[scroll_list.selectedIndex];
o.value = t_area.value;
o.text = t_area.text;
}
Anyhow, if you wanted to create a new option, use:

var updated = new Option(t_area.text, t_area.value, false, true);
(false sets the option's defaultSelected property to false, true sets
the option to selected).

scroll_list.options[scroll_list.selectedIndex] = updated;
}

but I get "Undefined" in the position with the new Option() (i.e.
"updated").


Because IE doesn't let you modify the properties of new options
directly, so either modify an existing option or use new Option() to set
the values when the option is created.
--
Rob
Jan 23 '06 #3
William wrote:
I tried to update a scrolling_list with the following:

function saveText( scroll_list, t_area, listToBeUpdated ) {
var updated = new Option();
updated.value = t_area.value;
updated.text = t_area.text;
scroll_list.options[scroll_list.selectedIndex] = updated;
}

but I get "Undefined" in the position with the new Option()
(i.e. "updated").

Why
Because you try to overwrite a reference to a DOM object with a
reference to another DOM object, which is not always possible.
and how to fix this?


function saveText(scroll_list, t_area, listToBeUpdated)
{
var updated = scroll_list.options[scroll_list.selectedIndex];
updated.value = t_area.value;
updated.text = t_area.text;
}
PointedEars
Jan 23 '06 #4

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

Similar topics

1
by: JKop | last post by:
Would you classify the following code as "Undefined Behaviour" or as "Non- portable"? signed main() { signed char chedder = 130; } Relevant information:
13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
3
by: Kenneth Kahl | last post by:
Hello, I would like to call a C++ programm out of Java with help of JNI. By the followed command I created a "shared library": g++ -shared -o libcalculate.so rechner.cpp When I create an...
9
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined...
5
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.