Kayo Kroeze
Kayo Kroeze

Reputation: 31

IE7 javascript error: runtime-error microsoft jscript: dropdownlist is not defined

I have another error caused by IE7 (great program...) I am trying to get a dropdownlist into a javascript function so that i can use it's values to hid some divs which are named after those values. but each time I try to use this dropdownlist I get the following error: runtime-error microsoft jscript: dropdownlist is not defined

the javascript:

<script src="/Scripts/ShowHide.js" type="text/javascript"></script>

function ShowHideDivByDropDownList(dropdownlist) {
  for (i = 0; i < dropdownlist.options.lenght; i++) {
      var divId = dropdownlist.options[i].value;
      if (divId != "") {
          document.getElementById(divId).style.display = "none";
      }
  }
  document.getElementById(drowdownlist.value).style.display = "block";
}

the dropdownlist:

@Html.DropDownList("MainList",
                    new SelectList(Model.ListCategories, 
                    Model.List,
                    new { onchange ="ShowHideDivByDropDownList(this)"})

EDIT:

I have made allot of trail adjustments to try and make the script running, allot of people seem to have noticed this :). I have returned to script to it's original state, but the error still occurs.

Upvotes: 2

Views: 765

Answers (3)

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

You can prevent yourself all this mess - as this answer correctly said, you have to use getElementById but if you change your code to this:

onchange ="ShowHideDivByDropDownList(this)"

Then you pass the actual object to the function then you can safely have such code instead:

function ShowHideDivByDropDownList(drowdownlist) {
    for (var i = 0; i < drowdownlist.options.length; i++) {
        var divId = drowdownlist.options[i].value;
        if (divId !== "") {
            var element = document.getElementById(divId);
            if (element)
                element.style.display = "none";
        }
    }
    var element = document.getElementById(drowdownlist.value);
    if (element)
        element.style.display = "block";
}

Couple of things I fixed along the way as well:

  1. In JavaScript, array length is .length, not .count
  2. In case there's no element with such ID your code would crash - to avoid such mishap it's always good practice to validate you really have such element - you can add alert("element does not exist"); for debug purpose but having the whole code crash because you have typo is not a good thing.

Upvotes: 0

sinsedrix
sinsedrix

Reputation: 4795

If it's an ID use getElementById(id), if it's a name use getElementsByName(name)[0].

getElementByName doesn't exist.

Also be careful with your variable names...

Upvotes: 1

Josh Mein
Josh Mein

Reputation: 28655

In your for loop you have drowdownlish instead of drowdownlist. Just for sanity sake you may want to make those dropdownlist.

function ShowHideDivByDropDownList(dropdownlistid) {
  var dropdownlist= document.getElementByName(dropdownlistid);
  for (i = 0; i < dropdownlist.options.count; i++) {
      var divId = dropdownlist.options[i].value;
      if (divId != "") {
          document.getElementById(divId).style.display = "none";
      }
  }
  document.getElementById(dropdownlist.value).style.display = "block";
}

Upvotes: 0

Related Questions