473,387 Members | 1,585 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 does event for SELECT not fire in Mozilla?

Why does this not work in Mozilla ?

<http://homepage.ntlworld.com/mark.pawelek/code/animals.html>

The optHabitat_change() event does not fire. What am I doing wrong
here?

PS: It should repopulate the 2nd combo based upon the value of the
selected item in the first, just like it does in IE.

Jul 23 '05 #1
2 3472
[Follow-ups set to comp.lang.javascript]

On Fri, 24 Sep 2004 10:02:55 +0100, mark4asp
<ma****************@ntlworld.com> wrote:
Why does this not work in Mozilla ?

<http://homepage.ntlworld.com/mark.pawelek/code/animals.html>

The optHabitat_change() event does not fire. What am I doing wrong
here?


It does fire. If you look at the error console, you'll see Mozilla
complain.

You see unfortunately, IE hasn't followed the W3C with regards to the
HTMLSelectElement.add method. IE uses a number for the second argument,
and an optional one at that. However, the W3C DOM states that the second
argument is a required object reference.

The two simply aren't compatible (though good 'ol Opera allows both).

There seems to be four options available.

1) Fall back on the old approach of using the Option constructor to create
new OPTION elements and append them using the options collection. This
will be supported by older scriptable browsers.
2) Use try/catch to determine whether an error occurs whilst trying to use
the object or number version of the method. Use the other approach in the
catch clause. This won't be supported by older browsers because a) they
don't support try/catch, and b) they won't support DOM 1.
3) Use appendChild to add the OPTION elements. I haven't thoroughly tested
this, but it appears to work. This won't be supported by older browsers as
they don't support DOM 1.
4) Use the (preferred) server-side approach. This will be supported by old
and new browsers alike, scriptable or otherwise.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
On Fri, 24 Sep 2004 09:35:01 GMT, "Michael Winter"
<M.******@blueyonder.co.invalid> wrote:
[Follow-ups set to comp.lang.javascript]

On Fri, 24 Sep 2004 10:02:55 +0100, mark4asp
<ma****************@ntlworld.com> wrote:
Why does this not work in Mozilla ?

<http://homepage.ntlworld.com/mark.pawelek/code/animals.html>

The optHabitat_change() event does not fire. What am I doing wrong
here?


It does fire. If you look at the error console, you'll see Mozilla
complain.

You see unfortunately, IE hasn't followed the W3C with regards to the
HTMLSelectElement.add method. IE uses a number for the second argument,
and an optional one at that. However, the W3C DOM states that the second
argument is a required object reference.

The two simply aren't compatible (though good 'ol Opera allows both).

There seems to be four options available.

1) Fall back on the old approach of using the Option constructor to create
new OPTION elements and append them using the options collection. This
will be supported by older scriptable browsers.
2) Use try/catch to determine whether an error occurs whilst trying to use
the object or number version of the method. Use the other approach in the
catch clause. This won't be supported by older browsers because a) they
don't support try/catch, and b) they won't support DOM 1.
3) Use appendChild to add the OPTION elements. I haven't thoroughly tested
this, but it appears to work. This won't be supported by older browsers as
they don't support DOM 1.
4) Use the (preferred) server-side approach. This will be supported by old
and new browsers alike, scriptable or otherwise.

Hope that helps,
Mike


It helped but the code still didn't work in Mozilla - for a different
reason which I was never able to figure out!

I fixed it by passing in the value of the item to be selected instead
of the index to be selected into the two functions loadOpt() and
optHabitat_change(). This is much more logical even if it needs a line
or two of code more. I dispense with the stupid index positions and
use the values (which are the numbers in my data arrays anyway).

I include the fixed code because I always like a complete solution for
the benefit of any stranger who may have a similar problem in future.
[Oh yeh, and I've decided that Google is a nice place to store bits of
code that one may need in future too!]

I also had to change the way options were deleted as well as created.

<html>
<head>
<title>Funny Animals</title>
<script language="javascript" type="text/javascript">
<!--
// Start of database
habitats = {
1:"arctic",
2:"desert",
3:"ocean" }

// "name", "habitat", "type", "page #"
creatures = {
1:["camel", "2", "10", "1¸14"],
2:["polar bear", "1", "10", "1¸14"],
3:["scorpion", "2", "10", "1¸14"],
4:["tuna", "3", "10", "1¸14"],
5:["whale", "3", "", "9"] }

// Initialise HTML
window.onload = Initialise;

// Core code
function getObj(oTxt) {
return document.getElementById(oTxt);
}

function toUpper(w) {
return (w.charAt(0).toUpperCase() + w.substring(1))
}

function Int(n){
return parseInt(n);
}
// Core code END

// Initialise & Events
function Initialise(){
loadOpt(getObj('optHabitat'), habitats, 1);
optHabitat_change(getObj('optCreature'), 1);
}

// selVal = value of row to be selected.
function loadOpt(oCbo, ary, selVal) {
var key;
var i = 0;
for (key in ary) {
oCbo.options[oCbo.options.length] = new Option(toUpper(ary[key]),
key);
oCbo[i].selected = (oCbo.options.value==selVal) ? true : false;
i++;
}
}

function makeOption(obj, text) {
if (obj!=null && obj.options!=null)
obj.options[obj.options.length] = new Option(text, value)
}

function removeAllOptions(oCbo) {
for (var i=(oCbo.options.length-1); i>=0; i--)
oCbo.options[i] = null;
oCbo.selectedIndex = -1
}

function optHabitat_change(oCbo, habitat) {
var s = '';
var key;
removeAllOptions(oCbo);
for (key in creatures) {
s = creatures[key][1].toString();
arow = s.split('¸');
if (habitat==Int(arow[0]))
oCbo.options[oCbo.options.length] = new
Option(toUpper(creatures[key][0]), key);
}
return true
}

// Initialise & Events END

//-->
</script>
</head>

<body>
<p>Creatures:<br />
<select id="optHabitat"
onchange="optHabitat_change(getObj('optCreature'), this.value)"></select><br>
<!-- ,this.options(this.selectedIndex).value -->
<select id="optCreature"></select></p>
</body>
</html>
Jul 23 '05 #3

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

Similar topics

4
by: Brian Glen Palicia | last post by:
My goal is to accept input from the user into a text box and then parse the data using split(). The first step is this tiny program to test the split() function. It runs in IE, but in Mozilla it...
9
by: chandramohan.mani | last post by:
Does Event handlers work in netscape. If yes means can anyone please help me. <HTML><SCRIPT LANGUAGE="JScript"> function mouseclick() { alert("I was clicked on " +...
1
by: Duwayne | last post by:
I am having a lot of trouble with an ascx page that has a textbox that *should fire a textchange event when the text changes. The main aspx page dynamically loads the ascx page and has a image...
0
by: Charles Law | last post by:
Is there a way to get the MouseEnter event to fire for a user control that inherits from UpDownBase? The event is hidden, and does not fire when the mouse enters the control. TIA Charles
8
by: ApexData | last post by:
I'm using the OnKeyDown event in hopes of capturing a key being held down during the initial startup of my application. Does anyone know, at what point the OnKeyDown event fires? I checked to...
1
by: John Dalberg | last post by:
What causes a server event to fire when something happens on the client? Say a user changed the gridview's page index, the server side PageIndexChanged event is fires. What makes...
0
by: manywolf | last post by:
I have an aspx page that fires the page load event twice for every load. I tried every fix that was suggested in all the posts on this and other forums. None changed the behavior. After one post that...
6
by: danielalrease | last post by:
The flash does not appear in mozilla fire fox even i have been downloaded the adobe, but i able to view it in IE. anyone can help on this issue ? website : www.ring8.com.my
6
by: Harry | last post by:
Hi All VS 2008 Pro It appears that the form_closing event of a form may not always fire. I put an explicit lock on a customer record when it is open for edit and then remove the lock in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.