Questions

Javascript getElementByID strangeness?

+
0 Votes
Locked

Javascript getElementByID strangeness?

chris
Does anyone have experience with Javascript to provide an answer to this simple code issue?

Why does getElementByID not work in my case with Opera and Firebox but it does with IE?? I thought this method was cross browser compatible.

My test alerts simply do not fire in Opera or Firefox which results in the rest of my validation code failing too.

I'm calling this function from the onChange event on a field within a form.

Can anyone shed any light on this as it's baffling me!

strCtrl can be name of any field which I want to validate

function validate_ctrl(strCtrl)
{
if (strCtrl=='Name') {

// get handle on control
var ctrl = document.getElementById(strCtrl);
alert("Test"); // this fires in IE 7, Opera9.6, Firefox3.6
alert(ctrl.value); // this fires only in IE 7! Why???

//due to this none of the follow will work in Opera or Firefox?
// validate control
if (ctrl.value.length > 3) {
//color TD BG green
document.getElementById("Step1").style.backgroundColor = '#009900';
} else {
// not valid so color TD BG grey
document.getElementById("Step1").style.backgroundColor = '#999999';
}
}
//validate other controls here
//etc etc
}