Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Opera problems with element names

    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?

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.
    Code:
    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;
    }

  • #3
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •