简体   繁体   中英

onclick javascript function not working on a div element

I recently started learning coding and this is what i tried and got stuck at. The Javascript code functionality (on click) does not work on the div element. Anything wrong with the code? please help!

 <script type="text/javascript">

        var start = new Date().getTime();
        document.getElementById("rectangle").oncick = function() {
        document.getElementById("rectangle").style.display = "none";
        var end = new Date().getTime();
        var timetaken = (end - start) / 1000;
        document.getElementById("timetaken").innerHTML = timetaken + "s";

     }
 </script>

Welcome to SO! You have oncick instead of onclick :)

If you could post the rest of your code, it would help, but right off-hand I see that your onclick is misspelled as "oncick"

Honestly, take a look at the Console when running this in any modern web browser. You should see this as an error in the code right away.

Also, take a look at the jQuery framework -- it'll really tighten up the code here and helps minimize a lot of the grunt work.

Happy Coding!

misspell of onClick,use vscode and few plugins for better coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM