am I being stupid?

H

harry

Trying to get a function working in IE 5.5 (sp3) & Mozilla1.6

The function is called when a table row is double clicked i.e...

function dblClicked()
{
getElement("viewSearch").click();
}

This works fine under IE but not Mozilla, so I thought lets break the
statement down like so -

function dblClicked()
{
var x = getElement("viewSearch");
x.click():
}

This now doesn't work in either!

getElement() looks like this -

function getElement(id)
{
var e = document.getElementById(id);

if(e)
{
return e;
}
else
{
alert(e + " not found!");
}
}

Any ideas? - surely they are identical?

thanks

harry
 
M

Michael Winter

Trying to get a function working in IE 5.5 (sp3) & Mozilla1.6

I see two problems:
The function is called when a table row is double clicked i.e...

function dblClicked()
{
getElement("viewSearch").click();
}

1) Table rows do not have click() methods.

Microsoft might like to think differently (not surprising, really), but
most browsers will follow the DOM and only supply click() methods where it
makes sense: form controls. If you want to call click event listeners,
either dispatch a click event to the appropriate element, or call the
function referenced by onclick directly. The most appropriate will depend
on how you've added listeners.
This works fine under IE but not Mozilla, so I thought lets break the
statement down like so -

function dblClicked()
{
var x = getElement("viewSearch");
x.click():
}

2) You're not programming very defensively.

Your getElement() function might return undefined or null, but you don't
test for it. Make sure you do!

var x = getElement( 'viewSearch' );
if( x ) {
// Use x
}
This now doesn't work in either!

getElement() looks like this -

function getElement(id)
{
var e = document.getElementById(id);

if(e)
{
return e;
}
else
{
alert(e + " not found!");
}
}

A better way to write this would be:

var getElement;
if( document.getElementById ) {
getElement = function( id ) {
return document.getElementById( id );
};
} else if( document.all ) {
getElement = function( id ) {
return document.all[ id ];
};
} else {
getElement = function() {
return null;
};
}

By the way, your alert doesn't make much sense, but even if it did, it
wouldn't be of much use to the user. If e is null of undefined, your
message will display: "undefined not found!" That means nothing to you or
anyone else. Even if you change it to: alert( id + ' not found!'), it's
useless for the user.
Any ideas? - surely they are identical?

Abandon the direct click() approach as it won't be supported by that many
browsers. If you have trouble with the indirect method, do ask for more
help.

Good luck,
Mike
 
H

harry

Thanks for that Michael, don't suppose you would have some sample code to
"dispatch a click event to the appropriate element" ?

cheers

harry

Michael Winter said:
Trying to get a function working in IE 5.5 (sp3) & Mozilla1.6

I see two problems:
The function is called when a table row is double clicked i.e...

function dblClicked()
{
getElement("viewSearch").click();
}

1) Table rows do not have click() methods.

Microsoft might like to think differently (not surprising, really), but
most browsers will follow the DOM and only supply click() methods where it
makes sense: form controls. If you want to call click event listeners,
either dispatch a click event to the appropriate element, or call the
function referenced by onclick directly. The most appropriate will depend
on how you've added listeners.
This works fine under IE but not Mozilla, so I thought lets break the
statement down like so -

function dblClicked()
{
var x = getElement("viewSearch");
x.click():
}

2) You're not programming very defensively.

Your getElement() function might return undefined or null, but you don't
test for it. Make sure you do!

var x = getElement( 'viewSearch' );
if( x ) {
// Use x
}
This now doesn't work in either!

getElement() looks like this -

function getElement(id)
{
var e = document.getElementById(id);

if(e)
{
return e;
}
else
{
alert(e + " not found!");
}
}

A better way to write this would be:

var getElement;
if( document.getElementById ) {
getElement = function( id ) {
return document.getElementById( id );
};
} else if( document.all ) {
getElement = function( id ) {
return document.all[ id ];
};
} else {
getElement = function() {
return null;
};
}

By the way, your alert doesn't make much sense, but even if it did, it
wouldn't be of much use to the user. If e is null of undefined, your
message will display: "undefined not found!" That means nothing to you or
anyone else. Even if you change it to: alert( id + ' not found!'), it's
useless for the user.
Any ideas? - surely they are identical?

Abandon the direct click() approach as it won't be supported by that many
browsers. If you have trouble with the indirect method, do ask for more
help.

Good luck,
Mike
 
M

Michael Winter

Thanks for that Michael, don't suppose you would have some sample code to
"dispatch a click event to the appropriate element" ?

Not off-hand, no.

The first technique, a direct call, is the easiest and most compatible
approach. If you attached an event listener using either event properties
(element.onclick, etc.) or HTML attributes you write, quite simply:

element.eventName();

Using an example from your original post, would call the click event
listener by

var e = getElement( 'viewSearch' );
if( e && e.onclick ) {
e.onclick();
}

However, there is a caveat: any listener you call in this fashion must not
try to access an event object as there won't be one. Most of the time,
this won't be a problem, but do be careful.

The second technique, dispatching an event, is necessary if you use
Microsoft's attachEvent() method or the DOM's addEventListener() method,
but it requires more recent browsers and a lot more work. I'd prefer not
to include details of this at the moment, but if you need more
information, ask.

[snip]

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top