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.