473,387 Members | 1,745 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,387 software developers and data experts.

Why doesn't this work in Firefox?

Here's a simple script to update the contents of one select based on
the user's choice in a second select. It works fine in IE, but not at
all in Firefox. Can anyone shed light on the matter? Thanks.
Brad Smith
<html>
<head>
<title>Interests</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<script Language="JavaScript">
var i;
function AddItem(){
var oOption = document.createElement("OPTION");
oOption.text=document.myForm.SelectInterest.option s[document.myForm.SelectInterest.selectedIndex].text;
oOption.value=document.myForm.SelectInterest.optio ns[document.myForm.SelectInterest.selectedIndex].value;
document.myForm.Interests.add(oOption);
document.myForm.SelectInterest.selectedIndex=0;
}
</script>
<form name="myForm" method="post" action="">
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>
<select name="SelectInterest" onChange="AddItem()">
<option>Please make a selection</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>
<br>
<br>
<select name="Interests" size="10" >
</select>
</p>
<input name="Clear List" type="button"
onClick="document.myForm.Interests.options.length = 0 " value="Clear
List">
</form>
</body>
</html>

Jan 23 '06 #1
4 7235
Just figured it out. IE and Firefox require different argument subsets
for the .add method. This code tries one add method and if there's an
error, tries a backup method. I found the answer on
http://www.webdeveloper.com/forum/ar...p/t-56561.html
--pbs

<script Language="JavaScript">
var i;
function AddItem(){
var oOption = document.createElement("OPTION");
oOption.text=document.myForm.SelectInterest.option s[document.myForm.SelectInterest.selectedIndex].text;
oOption.value=document.myForm.SelectInterest.optio ns[document.myForm.SelectInterest.selectedIndex].value;
addOption(document.myForm.Interests, oOption);
document.myForm.SelectInterest.selectedIndex=0;
}
// Add a new option to a select element
function addOption(selectElement,newOption) {
// First try the DOM2 method ...
try {
selectElement.add(newOption,null);
}
// ... And if that doesn't work use the IE-only method
catch (e) {
selectElement.add(newOption,selectElement.length);
}
}

</script>

Jan 23 '06 #2
pa**************@gmail.com wrote:
Here's a simple script to update the contents of one select based on
the user's choice in a second select. It works fine in IE, but not at
all in Firefox. Can anyone shed light on the matter? Thanks.


You're using IE specific code that Firefox doesn't support.

Jan 23 '06 #3
pa**************@gmail.com wrote:
Just figured it out. IE and Firefox require different argument subsets
for the .add method. This code tries one add method and if there's an
error, tries a backup method. I found the answer on
http://www.webdeveloper.com/forum/ar...p/t-56561.html
--pbs

<script Language="JavaScript">
The language attribute is deprecated, type is required:

<script type="text/javascript">

var i;
function AddItem(){
var oOption = document.createElement("OPTION");
oOption.text=document.myForm.SelectInterest.option s[document.myForm.SelectInterest.selectedIndex].text;
oOption.value=document.myForm.SelectInterest.optio ns[document.myForm.SelectInterest.selectedIndex].value;
You can make these more efficient by getting references and reusing them:

var sel = document.myForm.SelectInterest;
var selOpt = sel.options[sel.selectedIndex];
Now you can get to the text and values as selOpt.text and selOpt.value,
and you have a reference to the select too as 'sel'.

addOption(document.myForm.Interests, oOption);
document.myForm.SelectInterest.selectedIndex=0;
}
// Add a new option to a select element
function addOption(selectElement,newOption) {
// First try the DOM2 method ...
try {
selectElement.add(newOption,null);
}
// ... And if that doesn't work use the IE-only method
catch (e) {
selectElement.add(newOption,selectElement.length);
}
}


No need for that, try..catch is an absolute last resort. Use the DOM 0
new Option:

function AddItem()
{
var sel = document.myForm.SelectInterest;
var selOpt = sel.options[sel.selectedIndex];
sel.options[sel.options.length] = new Option(selOpt.text,
selOpt.value);
sel.selectedIndex=0;
}
Untested but should be OK.
[...]

--
Rob
Jan 23 '06 #4
On 2006-01-23, pa**************@gmail.com <pa**************@gmail.com> wrote:
Just figured it out. IE and Firefox require different argument subsets
for the .add method. This code tries one add method and if there's an
error, tries a backup method. I found the answer on
http://www.webdeveloper.com/forum/ar...p/t-56561.html


:)

I just used the appendChild method it works the same in both.

Bye.
Jasen
Jan 24 '06 #5

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

Similar topics

6
by: Cliff R. | last post by:
Hi, I use a handy little Javascript Flash detection script on a number of sites (copied below). Usually works great, but I just started trying Firefox and it's not working. A few browsers are...
3
by: niconedz | last post by:
Hi The following code works fine in IE but not Firefox. It's a little script that zooms an image and resizes the window to fit. Can anybody tell me what's wrong? Thanks Nico == btw.....
4
by: Joe | last post by:
Hello, I have created a login page using dotnet. I am using requiredFieldValidator and noticed that the code works fine in IE but not in Netscape, Opera, Mozilla, Firefox, etc. For example...
4
by: bbp | last post by:
Hello, In an ASPX page I have a "Quit" button which make a simple redirect in code-behind. This button doesn't work no more since (I think) I moved from the framework 1.0 to 1.1 and it doesn't...
45
by: Pat | last post by:
its seems asp.net validation doesn't fire when using FireFox? Tested a page and it doesn't fire. It seems the javascript doesn't fire Any ideas?
21
by: briggs | last post by:
<html> <head> <script> /* AddChild */ function ac() { var c; var p = document.getElementById("p"); for (var i = 0; i < 5; i++) { c = document.createElement("DIV"); // Create 'div' element.
23
by: wylbur37 | last post by:
I'm running an Apache server on my own computer (Windows XP Pro). I wrote a simple PHP script (called test3.php) that I'm running by putting the following URL in the address bar of the browser...
2
bilibytes
by: bilibytes | last post by:
Hi, i have been creating an ajax aplication on my website and it worked as i expected it to work on Safari and Firefox, until i upgraded to Firefox 3. It doesn't work either on windows. the...
7
by: John Kotuby | last post by:
Hi all, I have created an ASPX page in VS 2008 that appears inside of an IFRAME inside a standard HTML page. Because it loads from an external site and is database driven, the first load takes a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.