javascript - Script works in Chrome but not IE - Stack Overflow

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

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";

        }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742262817a4411227.html

相关推荐

  • javascript - Script works in Chrome but not IE - Stack Overflow

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

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信