Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot set property 'onkeydown' of undefined

I'm trying to enable the tabulation module in my project. Working module I took from http://css-tricks.com/snippets/javascript/support-tabs-in-textareas/ , but when I add this .js to my project i get the following error:

Uncaught TypeError: Cannot set property 'onkeydown' of undefined. 

Its work when i run it on http://jsfiddle.net/zQgTx/ but doesnt work, when I run it in my project. Using jquery-1.9.1. My html code:

 <form method="POST" action="." class="navbar-form">
    <textarea id="answer" name="answer" style="width: 700px; height: 250px;"></textarea>
    <br><input class="btn btn-primary" type="submit" value="Check">
 </form>

Help solve the problem. Thanks!

like image 908
aphex Avatar asked Nov 20 '25 04:11

aphex


1 Answers

Wrap all your code in onload function like this:

window.onload = function(){

   // Your code;

}

Issue happens because when this line runs :

var textarea = document.getElementsByTagName('textarea')[0]; 

your textarea is not actually loaded. onload will solve it.

like image 182
Ivan Chernykh Avatar answered Nov 22 '25 18:11

Ivan Chernykh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!