document.getElementById shortcut
by
, 03-22-2012 at 08:38 AM (43867 Views)
I'm amazed at how many people don't use a very simple shortcut for document.getElementById. This is for all the people who don't use a javascript library like jQuery.
All you have to do is include this code somewhere on your page
Then when you want to useCode:function $(element) { return document.getElementById(element); }document.getElementById('id');
just type $('id'); instead.
Example
I think it's a real time saver and more people should use it.Code:<html> <head> <script type="text/javascript"> function $(element) { return document.getElementById(element); } </script> </head> <body> <input type="text" value="Hello" id="field1"> <input type="button" onclick="alert($('field1').value)"> </body> </html>