admin管理员组

文章数量:1293366

I feel like this should be the easiest thing in the world. I just want to make a button with an onclick call and have it run javascript. Yet every time I do that I get the Uncaught TypeError: undefined is not a function error.

I've read up on this but don't seem to understand why it can't find the function.

To recap: Hitting the button should run ThePush() but I get an error on the console.

Code:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

        function ThePush() {
          var name = document.getElementByID('name').value;
          var pl = document.getElementByID('PL1').value;
          var plw = document.getElementByID('theplw').value;
          var al = document.getElementByID('theal').value;
          //do stuff with these variables.
        }

</script>
</head>
<body>
<b>Player One: </b><input id="name" type="text" value="Name"></input>
<br/><b>Level: </b><input id="PL1" style="width:50px" type="text" id="PL1L" value="0" readonly></input>
<br/>
<button id="thesub" onclick="ThePush();">Submit</button>

<select id="theplw">
<option value="A: 0-999">A: 0-999</option>
<option value="B: 1,000-9,999">B: 1,000-9,999</option>
<option value="C: 10,000-99,999">C: 10,000-99,999</option>
<option value="D: 100,000-499,999">D: 100,000-499,999</option>
<option value="E: 500,000-1,499,999">E: 500,000-1,499,999</option>
<option value="F: 1,500,000+">F: 1,500,000+</option></select>
<br/><b>Sub Level: </b><input id="theal" style="width:50px" type="text" id="AL1L" value="0" readonly></input>
<hr>
<iframe src="others.php" frameborder="0" width="500"></iframe>
<iframe id="updater" src="update.php" frameborder="0" width="0" height="0"></iframe>

</body>
</html>

Any thoughts? Thank you in advance for your help!

I feel like this should be the easiest thing in the world. I just want to make a button with an onclick call and have it run javascript. Yet every time I do that I get the Uncaught TypeError: undefined is not a function error.

I've read up on this but don't seem to understand why it can't find the function.

To recap: Hitting the button should run ThePush() but I get an error on the console.

Code:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

        function ThePush() {
          var name = document.getElementByID('name').value;
          var pl = document.getElementByID('PL1').value;
          var plw = document.getElementByID('theplw').value;
          var al = document.getElementByID('theal').value;
          //do stuff with these variables.
        }

</script>
</head>
<body>
<b>Player One: </b><input id="name" type="text" value="Name"></input>
<br/><b>Level: </b><input id="PL1" style="width:50px" type="text" id="PL1L" value="0" readonly></input>
<br/>
<button id="thesub" onclick="ThePush();">Submit</button>

<select id="theplw">
<option value="A: 0-999">A: 0-999</option>
<option value="B: 1,000-9,999">B: 1,000-9,999</option>
<option value="C: 10,000-99,999">C: 10,000-99,999</option>
<option value="D: 100,000-499,999">D: 100,000-499,999</option>
<option value="E: 500,000-1,499,999">E: 500,000-1,499,999</option>
<option value="F: 1,500,000+">F: 1,500,000+</option></select>
<br/><b>Sub Level: </b><input id="theal" style="width:50px" type="text" id="AL1L" value="0" readonly></input>
<hr>
<iframe src="others.php" frameborder="0" width="500"></iframe>
<iframe id="updater" src="update.php" frameborder="0" width="0" height="0"></iframe>

</body>
</html>

Any thoughts? Thank you in advance for your help!

Share asked Jan 6, 2015 at 5:24 AtlasOnEarthAtlasOnEarth 831 gold badge2 silver badges7 bronze badges 2
  • there is nothing called getElementByID in javascript. you are suppose to use getElementById. – Suchit kumar Commented Jan 6, 2015 at 5:33
  • First off: getElementById, NOT getElementByID, second: <input/> NOT <input></input> (self closing tag), third: <hr/> (self closing just like <br/>), fourth: you should use the label tag when dealing with <input/> tags; for more info: w3schools./tags/tag_label.asp --> "The for attribute of the <label> tag should be equal to the id attribute of the related element to bind them together." – ctwheels Commented Jan 6, 2015 at 5:35
Add a ment  | 

3 Answers 3

Reset to default 5

You should change your method name to getElementById, not getElementByID.

Your onclick is fine, but getElementByID is not a function and is throwing your error. You are looking for getElementById

    function ThePush() {
      var name = document.getElementById('name').value;
      var pl = document.getElementById('PL1').value;
      var plw = document.getElementById('theplw').value;
      var al = document.getElementById('theal').value;
      //do stuff with these variables.
    }

I just stumbled on the same error. I really can't explain why yet (don't want to yet) but when I changed the button id from updater it started working. Maybe same name for id and event function caused the error

本文标签: javascriptbutton onclick Undefined is not a functionStack Overflow