/    Sign up×
Community /Pin to ProfileBookmark

new Option in Firefox

Hi, I’m running into an apparent problem with Firefox. I’m trying to use javascript to modify options. It appears to be perfectly functional in IE. In Firefox however it doesn’t allow me to select anything except the first modified option and any options that didn’t get modified.

[code]<html>
<head>
<title>Test Stuff</title>
<SCRIPT>
function calc()
{
document.forms[‘testform’].testselect.options[0] = new Option(“value1″,”1”);
document.forms[‘testform’].testselect.options[1] = new Option(“value2″,”2″);
}
</SCRIPT>
</head>

<body onload=calc()>
<hr>
<form name=”testform” onchange=calc()>
<select name=”testselect”>
<option value=”first”>first option</option>
<option value=”second”>second option</option>
<option value=”third”>third option</option>
</select>
</form>
</body>

</html>[/code]

In this example, it will not allow me to select “value2”. What am I doing wrong?

Also, I’m trying to use this method to speed up calculations in IE. My form right now has a very large number of hidden selects that appear/disappear when an option is selected. I’m trying to get “new Option” to work so that it only uses one select which changes into a very large number of different options. If that makes sense – my question is, which method is likely to run faster in IE? I’m only concerned with the time it takes to make the calculations.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzOct 16.2006 — Looks like it's doing that because you effectively remove an option and put a new one in its place, so the option that you removed no longer had a selected attribute and the selectedIndex was being updated accordingly. Apparently IE did the opposite.

Try this:
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;title&gt;Test Stuff&lt;/title&gt;
&lt;script type="text/javascript"&gt;&lt;!--
function calc()
{
var sel = document.forms['testform'].testselect;
sel.options[0] = new Option("value1","1",false,sel.options[0].selected);
sel.options[1] = new Option("value2","2",false,sel.options[1].selected);
}
window.onload = calc;
// --&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form name="testform" action="#"&gt;&lt;div&gt;
&lt;select name="testselect" onchange="calc()"&gt;
&lt;option value="first"&gt;first option&lt;/option&gt;
&lt;option value="second"&gt;second option&lt;/option&gt;
&lt;option value="third"&gt;third option&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ionoauthorOct 16.2006 — Thank you very much.
×

Success!

Help @iono spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...