Forum Moderators: open

Message Too Old, No Replies

Dropdown field populated by Javascript (Not functioning with Firefox)

         

fototex

9:00 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



Hi Webmasters,

I have a Javascript on my credit card page which populates the elements of a dropdown field (The Year field).
It just works with Internet Explorer and not Firefox.

Here is the script:

<script type="text/javascript">
<%
Dim orderDateYearx
orderDateYearx = cStr(Year(Date))
%>

yy="<%=orderDateYearx%>";
yyyy=parseInt(yy);
yyyy=yyyy

var listDocUpload = document.getElementById("expDate2");
var optn = document.createElement("OPTION");
optn.value = yyyy;
yyyy=yyyy+""
optn.text = yyyy.replace("20","");
listDocUpload.options.add(optn);

yyyy=parseInt(yyyy)+1
var optn2 = document.createElement("OPTION");
optn2.value = yyyy;
yyyy=yyyy+""
optn2.text = yyyy.replace("20","");
listDocUpload.options.add(optn2);

yyyy=parseInt(yyyy)+1
var optn3 = document.createElement("OPTION");
optn3.value = yyyy;
yyyy=yyyy+""
optn3.text = yyyy.replace("20","");
listDocUpload.options.add(optn3);
</script>

The dropdown field in Firefox is just blank. I mean no elements reside within it. (but its clickable)

Any ideas why this dropdown field in Firefox is not going to be populated?
Thank you for all the comments.

astupidname

10:14 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



It probably has to do with how you are adding the option elements to the select. The 'add' method is by standard a member of the select object, plus the add method is treated differently in IE. Try using this function:
function addOption(select,option) {
try {
select.add(option,null); //standard method
} catch(er) {
select.add(option); //IE only
}
}

Like so:
var optn = new Option(yyyy.replace('20', ''), yyyy);
addOption(listDocUpload, optn);
//etc... repeat for others

daveVk

10:15 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



replace lines like this
listDocUpload.options.add(optn);
with
listDocUpload.options[listDocUpload.options.length]=optn;

fototex

6:49 am on Apr 24, 2010 (gmt 0)

10+ Year Member



Hi all,

It did work after I have added an ID to the select.

Thank you again for all the responses.