There are 100,500 articles on the Internet about how to hang up a click event.
What does it hang in the attributes of the DOM elements.
I try to follow the concept of "unobtrusive" js.
Therefore, the question is how to hang an event for example on a link.
I try this:

document.getElementById('bold').onclick = function(element){ mainArea.focus(); document.execCommand('bold', null, ''); element.preventDefault(); }; 

But something does not come out ...
Advise.
Thank!

  • And what does not work: document.execCommand or onclick ? Yes, and not element, but event - for semantics. =) - ling

3 answers 3

Try this:

 <head> <script> function assign() { document.getElementById('to').onclick = function(e) { alert('Hi!'); e.preventDefault(); return false; } } </script> </head> <body onload="assign();"> <a id="to" href="ya.ru">Click me</a> </body> 

    On jQuery

      <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"> </script> <script> $(document).ready(function(){ $("a").click(function(event){ alert("We are staying here..."); event.preventDefault(); }); }); </script> </head> <body> <a href="http://jquery.com/">Click me</a> </body> </html> 

      Wrap your $ (document) .ready () code and place it between <head> tags ... </ head>