473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

this.selectedIndex returns a number, but this.value returns ""

Does a SELECT element (listbox) need to be inside
a FORM element?

The code I'm playing with:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<script language="javascript" type="text/javascript">
/*
* ChangeBGColor(this)
*/
function ChangeBGColor(oSel) {
// FAILING IN IE...
//document.body.style.backgroundColor="#"+oSel.value ;
//
// alert(document.bgColor); // <--- WORKS IN IE and FF
// alert(oSel.value); // <-- FAILING IN IE
// FAILING IN IE...
// alert("oSel.options(oSel.selectedIndex).value = #" + oSel.options(oSel.selectedIndex).value);
if (document.bgColor) {
// FAILS in IE...
document.bgColor="#" + oSel.options(oSel.selectedIndex).value;
//alert(oSel.selectedIndex); // WORKS in IE. Returns index.
}
else {
//unsupported browser
alert("Your browser fails to fully support javascript.");
}
//alert(oSel.value); // FAILS in IE. Empty string (?).
//alert(oSel.name); // WORKS IN IE. Returns name of listbox.
return;
}
</script>
</head>
<body>
<p><select size="1" name="cbxSelectColor" tabindex="0"
title="Select A Color" onchange="javascript:ChangeBGColor(this);">
<option>002244</option>
<option>000000</option>
<option>ff0000</option>
<option>00ff00</option>
<option>0000ff</option>
<option>777777</option>
<option>003366</option>
<option>004488</option>
<option>0055AA</option>
</select></p>
</body>
</html>

Any suggestions as to what's going wrong? I tried it under both
the XHTML transitional and the strict doctypes.

--
Jim Carlock
Post replies to the newsgroup, thanks.
Nov 23 '05 #1
7 6884
well it should be [] not ():

oSel.options[oSel.selectedIndex].value

Nov 23 '05 #2
"matty" <un**********@gmail.com> wrote:
well it should be [] not ():

oSel.options[oSel.selectedIndex].value


Yeah, I tried the brackets first, but it was failing in IE, so I
proceded to the following Microsoft pages...
http://msdn.microsoft.com/workshop/a...cts/select.asp
http://msdn.microsoft.com/workshop/a...es/value_3.asp

So I tried it both ways. Thanks for catching that. It still
seems to be a problem.

oSel.name returns the name of the combobox (listbox).
oSel.selectedIndex returns the selected index.

oSel.value seems to return an empty string in IE. I should
have taken all the comments out of the code.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<script language="javascript" type="text/javascript">
/*
* ChangeBGColor(this)
*/
function ChangeBGColor(oSel) {
alert(oSel.value); // FAILS in IE. Empty string (?).
//alert(oSel.name); // WORKS IN IE. Returns name of listbox.
// ^^^^ --- alerts "cbxSelectColor"
return;
}
</script>
</head>
<body>
<p><select size="1" name="cbxSelectColor" tabindex="0"
title="Select A Color" onchange="javascript:ChangeBGColor(this);">
<option>002244</option>
<option>000000</option>
<option>ff0000</option>
<option>00ff00</option>
<option>0000ff</option>
<option>777777</option>
<option>003366</option>
<option>004488</option>
<option>0055AA</option>
</select></p>
</body>
</html>
--
Jim Carlock
Post replies to the newsgroup, thanks.
Nov 23 '05 #3

"Jim Carlock" <an*******@127.0.0.1> wrote in message
news:W2***************@tornado.tampabay.rr.com...
"matty" <un**********@gmail.com> wrote:

oSel.value seems to return an empty string in IE. I should
have taken all the comments out of the code.

<option>002244</option>


You have not defined any values in your options.

Tim
Nov 23 '05 #4
how did I miss that :D

Nov 23 '05 #5
"matty" <un**********@gmail.com> wrote:
how did I miss that :D


Bahh! It must've been all the comments in the first message.
They confused me as well.

//WORKS in IE and FF...
alert(oSel.options[oSel.selectedIndex].text);

<g> Thanks. I tried oSel.text and forgot to try the
oSel.options[oSel.selectedIndex].text. That works.

--
Jim Carlock
Post replies to the newsgroup, thanks.
Nov 23 '05 #6
Jim Carlock wrote:
"matty" <un**********@gmail.com> wrote:
how did I miss that :D

Bahh! It must've been all the comments in the first message.
They confused me as well.

//WORKS in IE and FF...
alert(oSel.options[oSel.selectedIndex].text);


According to the HTML 4 specification, the value of an option is the
content of its value attribute, if there is one, and if not, the
option's content (i.e. the text). So:

alert(oSel.options[oSel.selectedIndex].value);

Should work in your case ... but IE doesn't follow the spec.
[...]

--
Rob
Nov 23 '05 #7
Jim Carlock wrote:
Does a SELECT element (listbox) need to be inside
a FORM element?

The code I'm playing with:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<script language="javascript" type="text/javascript">
/*
* ChangeBGColor(this)
*/
function ChangeBGColor(oSel) {
// FAILING IN IE...
//document.body.style.backgroundColor="#"+oSel.value ;
//
// alert(document.bgColor); // <--- WORKS IN IE and FF
// alert(oSel.value); // <-- FAILING IN IE
// FAILING IN IE...
// alert("oSel.options(oSel.selectedIndex).value = #" + oSel.options(oSel.selectedIndex).value);
if (document.bgColor) {
That should probably be:

var docBody = document.body || document.documentElement;
if (docBody && docBody.bgColor){
...

// FAILS in IE...
document.bgColor="#" + oSel.options(oSel.selectedIndex).value;
docBody.bgColor = '#' ...

But why not include the '#' in the option value/text? Then you can also
have any valid CSS value in the option:

<option value="rgb(0,0,255)">Blue;
<option value="#f00">Red;
...

//alert(oSel.selectedIndex); // WORKS in IE. Returns index.
}
else {
//unsupported browser
alert("Your browser fails to fully support javascript.");
Whether a browser supports a particular DOM interface is no indication
of the extent of its support for JavaScript. The message might be
better as:

...( "Oops, no support for DOM Interface HTMLBodyElement");

}
//alert(oSel.value); // FAILS in IE. Empty string (?).
//alert(oSel.name); // WORKS IN IE. Returns name of listbox.
return;
}
</script>
</head>
<body>
<p><select size="1" name="cbxSelectColor" tabindex="0"
title="Select A Color" onchange="javascript:ChangeBGColor(this);">


There is no need for "javascript:" as part of the content of an onchange
attribute.

[...]
--
Rob
Nov 23 '05 #8

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

Similar topics

8
10394
by: Lyn | last post by:
I am trying to get my head around the concept of default, special or empty values that appear in Access VBA, depending on data type. The Access Help is not much (help), and the manual that I have...
81
7223
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
6
1602
by: Jax | last post by:
Custom control problem. I'm modding a textbox so that it will always have a "%" sign at the end of it. I have overrided the Text property to account for the "%" value within the textbox and have...
43
2681
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
61
3082
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With Nothing I assume no memory is set aside nor GC'ed But...
13
1533
by: Jim in Arizona | last post by:
I made a page with a gridview that has rows show a different color if a number in a column is greater than or equal to 45. I also did this conditional formatting for the column next to it. Here's...
1
2829
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
3
1087
by: krumblebunk | last post by:
Hello gurus, I am learning Python to take the place of Perl in my toolbox of bits and bobs, and writing something pretty simple in theory, but having a hard time in Python with it - I am using a...
4
4806
by: Syranthrax | last post by:
I know this question has been addressed, however none of the suggested fixes are resolving the issue. This one causes the error (initial value of txtPoint.Text is 35): Private Sub...
7
1947
by: Redbeard | last post by:
I have a memo field that is supposed to be in paragraph format (with no returns) but half of the records are set up as point form with multiple returns. I have figured out how to remove the returns...
0
7260
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
7384
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
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
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
4746
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.