472,992 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

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 6858
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
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
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
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
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
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
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
by: manchin2 | last post by:
Hi, Can anybody please provide the information about "&quot" and its use, if possible please provide an example. ...
3
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
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
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
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.