473,624 Members | 2,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find value of SELECT if VALUE attribute is missing

According to standards, if an option is selected and it has no VALUE
attribute, the contents of the option tag is to be submitted.
So:

<select name="sel">
<option selected>Value</option>
</select>

will submit as "sel=Value" .

Unfortunately, IE says that sel.options[sel.selectedInd ex].value == "",
whereas FF reports "Value".
So how can script accurately extract the value that will actually be
submitted?

I have written this code, which works:

function optionValue(opt ) {
if (opt.value!="") {
return opt.value;
}
if (!'value' in opt) {
return opt.text;
}
if (opt.outerHTML && opt.outerHTML.t est(/<[^>]+value\s*=/i)) {
return opt.value;
}
return opt.text;
}
String.prototyp e.test=function (regex) {
return regex.test(this );
}

Suggestions?

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jan 8 '07 #1
4 2460
Matt Kruse wrote:
According to standards, if an option is selected and it has no VALUE
attribute, the contents of the option tag is to be submitted.
So:

<select name="sel">
<option selected>Value</option>
</select>

will submit as "sel=Value" .

Unfortunately, IE says that sel.options[sel.selectedInd ex].value == "",
whereas FF reports "Value".
So how can script accurately extract the value that will actually be
submitted?

I have written this code, which works:

function optionValue(opt ) {
if (opt.value!="") {
return opt.value;
}
if (!'value' in opt) {
return opt.text;
}
if (opt.outerHTML && opt.outerHTML.t est(/<[^>]+value\s*=/i)) {
return opt.value;
}
return opt.text;
}
String.prototyp e.test=function (regex) {
return regex.test(this );
}

Suggestions?
This is what I've been doing, which seems to work pretty well:

var theRealValue = mySelect.value ||
mySelect.option s[mySelect.select edIndex].value ||
mySelect.option s[mySelect.select edIndex].childNodes[0].nodeValue;
Which will get either the value of the select (if it's reported), the
value of the selected index (if it's reported), or finally the contents
of the first node contained in the option tag (which should be a text
node containing its value, but then you have to be careful with your
markup or that will fail).

Since IE is really the only player that doesn't report the select's
value, you could probably replace the childNodes stuff with innerHTML
and be safe.

Jeremy
Jan 8 '07 #2
Jeremy wrote:
This is what I've been doing, which seems to work pretty well:
But it fails in a common case...
var theRealValue = mySelect.value ||
mySelect.option s[mySelect.select edIndex].value ||
mySelect.option s[mySelect.select edIndex].childNodes[0].nodeValue;
If I have:
<OPTION VALUE="">Test</OPTION>

then your code will fall into the third case and report "Test", which would
not be accurate.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jan 8 '07 #3
Matt Kruse wrote:
Jeremy wrote:
>This is what I've been doing, which seems to work pretty well:

But it fails in a common case...
>var theRealValue = mySelect.value ||
mySelect.optio ns[mySelect.select edIndex].value ||
mySelect.optio ns[mySelect.select edIndex].childNodes[0].nodeValue;

If I have:
<OPTION VALUE="">Test</OPTION>

then your code will fall into the third case and report "Test", which would
not be accurate.
True that.
Jan 8 '07 #4
Matt Kruse wrote:

Hi,
According to standards, if an option is selected and it has no VALUE
attribute, the contents of the option tag is to be submitted.
<snip>
Unfortunately, IE says that sel.options[sel.selectedInd ex].value == "",
whereas FF reports "Value".
So how can script accurately extract the value that will actually be
submitted?
<snip>
if (!'value' in opt) {
return opt.text;
}
I do not understand why you have included this condition ?
if (opt.outerHTML && opt.outerHTML.t est(/<[^>]+value\s*=/i)) {
The regexp should be solid enough I think, the only way I can imagine to
defeat it would be to use some custom attribute which would contain
"value=" inside it :)

My analysis is probably the same as yours. Basically, the code should
handle three mutually exclusive states:
- the value is there and is not empty,
- the value is there, however it is empty,
- the value is not there, the content becomes the real value.

The point is to distinguish between an empty value and no value at all.
Reading the value 'programmatical ly' does not help, because for each
case an empty string is returned. Therefore the script should not
attempt to use some 'programmatic' way to make the difference; using
outerHTML and parsing the serialized string of the option seems the
appropriate alternative.

Here's some function, also managing the "value=" slight issue.

---
function getOptionValue( opt){
var v=opt.value, t=opt.text;
return (v || attributeExists (opt,"value")) ? v : t;

function attributeExists (obj, attrName) {
var oHtml=obj.outer HTML;
var found=false;

if(oHtml)
found=/value\s*=/i.test(collapse QuotedValues(oH tml));

return found;

function collapseQuotedV alues(txt){
var sQuote=txt.inde xOf("'");
var dQuote=txt.inde xOf("\"");
var q="";

if(sQuote==-1 && dQuote!=-1) {
q="\"";
} else if(sQuote!=-1 && dQuote==-1) {
q="'"
} else if(sQuote!=-1 && dQuote!=-1) {
if(sQuote<dQuot e) q="'";
if(dQuote<sQuot e) q="\"";
}

if(q) txt=arguments.c allee(
txt.replace(new RegExp(q+"[^"+q+"]*"+q),"_")
);

return txt;
}

}
}
---
Kind regards,
Elegie.

PS: also, when the MULTIPLE attribute is set, there can be many values
for the SELECT.
Jan 8 '07 #5

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

Similar topics

3
4960
by: Jorn W Janneck | last post by:
hello everyone. i have the sort of question that makes me feel like i am missing the forest for the trees, so apologies if i am missing the blatantly obvious here. i am using saxon, and mostly xslt version 1.1 (the unofficial one). i have a template parameter, say v, which contains a sequence of nodes of the same kind, say A, which all have an attribute, say n. these attributes may have different numeric values. i have identified the...
0
1391
by: Petterson Mikael | last post by:
Hi, I have managed to get the name of the enum e.g. TxDeviceGroup_BbBusState in the first part of the xml in the class element. Now I need to find the minimum value of the enum called TxDeviceGroup_BbBusState in the xml. This is an operation I need to do for many enums. I thought of doing something like this:
2
3209
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
1
3010
by: Young-Soo Roh | last post by:
Hi. I need to get simple script working. I have many select object. When I select a different option, I would like to change the value of corresponding text input field. How can I do this using javascript? Here is what I have so far. <SELECT NAME=\"%s\" onChange=\"fill_desc(this.form, $name, $field_name)>
6
2192
by: Kurt | last post by:
Hello. I am writing a C# program that uses CDO 1.21 to access the outlook addressbook as a contact management system The following code gives me the correct dialog box, but the "Show Names from the:" drop down list box is empty I opened the correct default profile (i have only 1 profile) and I should see Global Address List (from the exchange server), and at least 1 Contact folder try Object argsAddressBook = new Object...
67
7674
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 4/30/06 I am looking for suggestions on how to find the date ranges where there were no transactions.
3
6438
by: 张韡武 | last post by:
Hello. I am a newbie trying to get my first XLST script working. I already know how to do this: Source XML: <category id="a0104"> <name>Oil and Gas</name> </category> XSLT: <xsl:for-each select="category">
2
1430
by: Hvid Hat | last post by:
Hi I've been messing around with adding attributes to certain nodes. I've looked at FAQ at http://www.dpawson.co.uk/ without finding what I'm looking for. Is it possible to have a template that is called with a node, an attribute name and an attribute value. The template would add the the given attribute name with the attribute value to the given note. Is this possible with XSLT - and if so, where can I get it? :-)
10
2186
by: Kelly | last post by:
Can anyone tell me what I'm doing wrong here? ================================== <html> <head> <title>Test Form</title> <script language="JavaScript" type="text/javascript"> </head>
0
8246
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1489
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.