From jquery back to JavaScript
I need a pure JavaScript equivalent of this Jquery code: because the use of libraries isnt allowed in my current project,
File handler.js
$(document).ready(function(){ $("#some_button").click(function(){ alert('ok. works'); }); });
File index.html
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="handler.js"></script> <head> <body> <button id="some_button">Btn</button> </body> </html>
Answers
Put this in your handler.js file:
window.onload = function() { document.getElementById('some_button').addEventListener('click', function() { alert('ok. works'); }, true); };
NB: this won't work on older IE versions that don't support addEventListener. You could be lazy and use the DOM0 element.onclick method instead.
function buttonClick() { alert('ok. works'); }
File index.html
<!DOCTYPE html> <html> <head> <head> <body> <button id="some_button" onclick="buttonClick()">Btn</button> </body> </html>