...

View Full Version : Opera problems with element names



doozer
03-17-2005, 12:36 AM
I have a problem with opera compatibility on a search engine page from my website.

My javascript is a function that validates the selections in groups of select boxes on a form.

Essentially if the first select box in a group is not set at 'selectedIndex 0' it checks the next two elements on the form for their values and returns an error if they are inappropriate.

if ( (form.elements[i].className.toLowerCase() == 'logic') &&
(form.elements[i].selectedIndex != 0) &&
( (form.elements[i+1].selectedIndex == 0) ||
( (form.elements[i].selectedIndex == 4) &&
( (form.elements[i+2].selectedIndex == 0) ||
(form.elements[i+1].selectedIndex > form.elements[i+2].selectedIndex)
)
)
) ||
( (form.elements[i].selectedIndex != 2) &&
(form.elements[i+1].selectedIndex == 11)
)
) {
alert ('There is an error with your ' + form.elements[i].id + ' selection!');
form.elements[i].focus();
return false;
}

In IE and FF this script correctly interprets form.elements[i+1] as the next element in the form but Opera gives me undefined errors with the script.

Is it just a sloppy syntax thing on my part or is there a way to define 'the next element on this form' in a way that all browsers will interpret in the same way? :confused:

glenngv
03-17-2005, 03:18 AM
What is the value of i, 0? Maybe you need to post the whole script and the form to easily verify the error.

I tidied up the code to make it more readable, efficient and easier to debug. I stored the current element and the selected indices of the 3 comboboxes in variables so you don't have to repetitively access them from the elements collection. BTW, you have a very complicated checking. :D

var elem = form.elements[i];
var elemIndex = elem.selectedIndex;
var elem1Index = form.elements[i+1].selectedIndex;
var elem2Index = form.elements[i+2].selectedIndex;
if
(
(elem.className.toLowerCase() == 'logic') &&
(elemIndex != 0) &&
(
(elem1Index == 0) ||
(
(elemIndex == 4) &&
(
(elem2Index == 0) ||
(elem1Index > elem2Index)
)
)
) ||
(
(elemIndex != 2) &&
(elem1Index == 11)
)
)
{
alert ('There is an error with your ' + elem.id + ' selection!');
elem.focus();
return false;
}

doozer
03-17-2005, 09:11 PM
Oh my God!

Thanks glenngv. You gave me the answer in your post but probably not in the way you intended.

"What is the value of i..?"

Precisely. I had set up a for loop using i to select an element for checking. As it got to the end of the loop obviously element i+1 and i+2 don't exist or as the error said 'were undefined'. I've now nested the if statements instead of stringing them together, and it isn't looking for phantom elements any more.

Thanks.

I have such a lot to learn about this programming malarky.
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum