最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

The click event from javascript on a button doesn't fire - Stack Overflow

programmeradmin7浏览0评论

So, I was going through a tutorial on event handlers. I created a button as instructed and then when I click on it, I wanted an alert to be displayed. Thats it. But it wouldn't work. Here's my html code:

<button id="submit">Submit</button>

And here's my javascript code:

var myButton = document.getElementByID("submit");
myButton.onclick = function(){
alert("YOu clicked on this button");
    }

I am using external js file and I've included it in the html from the head of the document.

So, I was going through a tutorial on event handlers. I created a button as instructed and then when I click on it, I wanted an alert to be displayed. Thats it. But it wouldn't work. Here's my html code:

<button id="submit">Submit</button>

And here's my javascript code:

var myButton = document.getElementByID("submit");
myButton.onclick = function(){
alert("YOu clicked on this button");
    }

I am using external js file and I've included it in the html from the head of the document.

Share Improve this question edited Aug 19, 2013 at 2:55 Steven V 16.6k3 gold badges66 silver badges77 bronze badges asked Aug 19, 2013 at 2:53 user2694983user2694983 31 silver badge3 bronze badges 3
  • It's close; use getElementById, not getElementByID – Chris Forrence Commented Aug 19, 2013 at 2:56
  • Use document.getElementById('submit'); instead – David Strada Commented Aug 19, 2013 at 2:56
  • 1 You need to learn to use your browser's error console. It would have told you that document.getElementByID is not a function. And once you fix that problem, it will tell you whatever else is going wrong. – ruakh Commented Aug 19, 2013 at 2:57
Add a ment  | 

5 Answers 5

Reset to default 10

document.getElementByID("submit"); -- it's Id instead of ID

Edit: I feel very bad for giving this one-liner as an answer, so to add to what others have said about learning how to use the browser's console as a debuggining tool, you should try to find an IDE/text editor with auto-pletion to save you such headaches especially when you're just starting out.

You may have an issue where document.getElementById() is happening before the element is created on the page. Try including your JavaScript in an onload event, or include it after the button in your HTML.

As previously stated, use document.getElementById("submit") with lower case Id. Also, you may want to use setAttribute so the alert fires when the button is pressed instead of immediately opening a popup when the alert line is encountered.

<script language="javascript">

    var myButton = document.getElementById("submit");
    myButton.setAttribute("onclick", "alert('You clicked on this button')");

</script>

change from document.getElementByID to document.getElementById and make sure your script stay below all the elements in the body. Example:

<body>
<button id="submit">Submit</button>

<script type="text/javascript">

    var myBtn = document.getElementById("submit");
    myBtn.onclick = function()
    {
       alert("this is a click event button");
    };

</script>

</body>

or you can put the script inside the <head></head> by add this event below to your script:

function initialize()
{
  // paste your code in here
}

document.addEventListener("DOMContentLoaded",initialize,false);

Hope it work!

it maybe a typo. It is getElementById not ID

发布评论

评论列表(0)

  1. 暂无评论