admin管理员组

文章数量:1335361

It's so simple, but I don't know why IE isn't doing my innerHTML changes and other stuff.

    function changeele2() {
        document.getElementById("eleme2");
        document.getElementById("workout");
        document.getElementById("workoutweek");

        if(workout.value == "Yes") { 
            eleme2.style.display = "inline-block";
            workoutweek.className += " requiredField";
        }

    }

It called if I change the value of a Dropdown:

  <select id="workout" onchange="changeele2()">
      <option>No</option>
      <option>Yes</option>
  </select>

Neither works a Button with Text

I just can't find it out. Has anyone got an idea?

It's so simple, but I don't know why IE isn't doing my innerHTML changes and other stuff.

    function changeele2() {
        document.getElementById("eleme2");
        document.getElementById("workout");
        document.getElementById("workoutweek");

        if(workout.value == "Yes") { 
            eleme2.style.display = "inline-block";
            workoutweek.className += " requiredField";
        }

    }

It called if I change the value of a Dropdown:

  <select id="workout" onchange="changeele2()">
      <option>No</option>
      <option>Yes</option>
  </select>

Neither works a Button with Text

I just can't find it out. Has anyone got an idea?

Share edited Jun 11, 2014 at 9:17 Hannes Schneidermayer asked Apr 17, 2013 at 22:27 Hannes SchneidermayerHannes Schneidermayer 5,7853 gold badges32 silver badges35 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

When you do document.getElementById("eleme2") you have to save the result of that operation and use that for subsequent access to that element.

function changeele2() {
    var eleme2 = document.getElementById("eleme2");
    var workout = document.getElementById("workout");
    var workoutweek = document.getElementById("workoutweek");

    if (workout.value == "Yes") { 
        eleme2.style.display = "inline-block";
        workoutweek.className += " requiredField";
    }
}

There are some browsers that make a global variable by the same name as the element id so that may be why it was sometimes working, but you should not rely on that.

This script shouldn't be working at all, chrome is salvaging it by looking up the elements by ID.

You should change it like this:

function changeele2()
    {
        var eleme2 = document.getElementById("eleme2");
        var workout = document.getElementById("workout");
        var workoutweek = document.getElementById("workoutweek");

        if(workout.value == "Yes") { 
        eleme2.style.display = "inline-block";
        workoutweek.className += " requiredField";

        }
}

本文标签: javascriptScript works in Chrome but not IEStack Overflow