473,396 Members | 2,109 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,396 software developers and data experts.

Javascript wierdness

When using this code, I can get the value of the select. But this only
works if the select is within a form. The commented code, which I think
_should_ work, does not work whether it's in a form or not. This happens
in both IE6 and Firefox 1.506 and also Flock 0.7.3.2.My javascript
skills are not all that great, but am I missing something?
Thanks

function setStatusText(){

//var index = document.getElementById('bridgeSelect').value;
var str= document.forms['test'].options.value;
document.getElementById('bridgeStatus').innerHTML = str;
}
####################
<body>
<form name='test'>
<div id="bridgeSelect"
onchange="toggleBridgeStatus(false);setStatusText( )"><select name="options">
<option value="status" selected="yes">Status
<option value="option1">Option1
<option value="option2">Option2
</select>
</div>
</form>
</body>
Aug 20 '06 #1
4 1084
Lorenzo Thurman wrote:
When using this code, I can get the value of the select. But this only
works if the select is within a form. The commented code, which I think
_should_ work, does not work whether it's in a form or not. This happens
in both IE6 and Firefox 1.506 and also Flock 0.7.3.2.My javascript
skills are not all that great, but am I missing something?
Yes, an understanding of HTML DOM element properties.

function setStatusText(){

//var index = document.getElementById('bridgeSelect').value;
The reason this line doesn't "work" is because
document.getElementById('bridgeSelect') returns a reference to a div
element, which is a standard HTML DOM element. If you look at the
properties of a normal HTML DOM element, you will see that it doesn't
have a "value" attribute/property:

<URL: http://developer.mozilla.org/en/docs...ent#Properties >

var str= document.forms['test'].options.value;
In this line, you are using the document's forms collection to get a
reference to the form named 'test', then to the select element named
'options' whose value attribute (in W3C compliant browsers) is the value
of the selected option.

The most widely supported method (though I've forgotten the precise
reason why it is used) is:

var sel = document.forms['test'].elements['options'];
var str = sel[sel.selectedIndex].value;
The first line could be abbreviated to:

var sel = document.test.options;
<URL:
http://www.w3.org/TR/DOM-Level-2-HTM...ionsCollection >
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-94282980 >
I would not recommend calling a select element 'options', it is
confusing to me as select elements have an options property that is a
collection of all the options.

document.getElementById('bridgeStatus').innerHTML = str;
}
####################
<body>
<form name='test'>
<div id="bridgeSelect"
onchange="toggleBridgeStatus(false);setStatusText( )"><select
name="options">
<option value="status" selected="yes">Status
<option value="option1">Option1
<option value="option2">Option2
</select>
</div>
</form>
</body>

--
Rob
Aug 20 '06 #2
RobG said the following on 8/19/2006 10:24 PM:

<snip>
The most widely supported method (though I've forgotten the precise
reason why it is used) is:

var sel = document.forms['test'].elements['options'];
var str = sel[sel.selectedIndex].value;
It is a throw back to the Netscape 4 days when NN4.xx browsers wouldn't
expose the value of a select element without going through the
collection. It is covered, lightly, in the FAQ section 4.13

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 20 '06 #3
Lee wrote:
Lorenzo Thurman said:
>When using this code, I can get the value of the select. But this only
works if the select is within a form. The commented code, which I think
_should_ work, does not work whether it's in a form or not. This happens
in both IE6 and Firefox 1.506 and also Flock 0.7.3.2.My javascript
skills are not all that great, but am I missing something?
Thanks

function setStatusText(){

//var index = document.getElementById('bridgeSelect').value;
var str= document.forms['test'].options.value;
document.getElementById('bridgeStatus').innerHTML = str;
}
####################
<body>
<form name='test'>
<div id="bridgeSelect"

bridgeSelect is a <DIVelement, and so doesn't have a "value".

How to get the value of a form control:
http://www.jibbering.com/faq/#FAQ4_13

Thanks, I guess I need to work on my DOM also.
Aug 20 '06 #4
RobG wrote:
Lorenzo Thurman wrote:
>When using this code, I can get the value of the select. But this only
works if the select is within a form. The commented code, which I
think _should_ work, does not work whether it's in a form or not. This
happens in both IE6 and Firefox 1.506 and also Flock 0.7.3.2.My
javascript skills are not all that great, but am I missing something?

Yes, an understanding of HTML DOM element properties.

>function setStatusText(){
//var index = document.getElementById('bridgeSelect').value;

The reason this line doesn't "work" is because
document.getElementById('bridgeSelect') returns a reference to a div
element, which is a standard HTML DOM element. If you look at the
properties of a normal HTML DOM element, you will see that it doesn't
have a "value" attribute/property:

<URL: http://developer.mozilla.org/en/docs...ent#Properties >

> var str= document.forms['test'].options.value;

In this line, you are using the document's forms collection to get a
reference to the form named 'test', then to the select element named
'options' whose value attribute (in W3C compliant browsers) is the value
of the selected option.

The most widely supported method (though I've forgotten the precise
reason why it is used) is:

var sel = document.forms['test'].elements['options'];
var str = sel[sel.selectedIndex].value;
The first line could be abbreviated to:

var sel = document.test.options;
<URL:
http://www.w3.org/TR/DOM-Level-2-HTM...ionsCollection >
<URL: http://www.w3.org/TR/DOM-Level-2-HTM...ml#ID-94282980 >
I would not recommend calling a select element 'options', it is
confusing to me as select elements have an options property that is a
collection of all the options.

> document.getElementById('bridgeStatus').innerHTML = str;
}
####################
<body>
<form name='test'>
<div id="bridgeSelect"
onchange="toggleBridgeStatus(false);setStatusText ()"><select
name="options">
<option value="status" selected="yes">Status
<option value="option1">Option1
<option value="option2">Option2
</select>
</div>
</form>
</body>

Thanks, I guess I need to work on my DOM also.
Aug 20 '06 #5

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

Similar topics

0
by: Matthew Alton | last post by:
The appended program freaks python 2.2 & 2.3 completely out. To reproduce the wierdness: i) copy the source to a file called consarn.py ii) $ python consarn.py; iii) the program is now doing a...
13
by: Tek Boy | last post by:
I've been experiencing some (reproducable) wierdness when I try to generate some very basic HTML using ASP. Check out the following (basic) ASP code: ===========================================...
0
by: amber | last post by:
Hello, I'm having some wierdness with a report I've created in VB.NET (with Crysal Reports). The report is called repCPDocSubmissionSummary.vb I created it a while ago, and have been using it...
5
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
2
by: JCE | last post by:
I need to programmatically invoke an asp:Button click event from a javascript function. The page containing the script and the button is the HTML page associated with a WebUserControl-derived...
9
by: Bobby Edward | last post by:
Are there any add-ons to Visual Studio 2008 that will help me troubleshoot CSS wierdness? Like showing the padding/margin with difference colors, etc...? I'm trying to figure out some wierd...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...

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.