formations/javascript/base/api-dom-4.md

32 lines
600 B
Markdown
Raw Permalink Normal View History

# .cadoles-slide-title[API du DOM (4/5)]
**Gestion des évènements**
```html
<html>
<head>
</head>
<body>
<button id="my-button" onclick="myButtonClickHandler()"></button>
</body>
</html>
```
```js
var myButton = document.getElementById('my-button');
// Méthode classique via les attributs (voir HTML)
function myButtonClickHandler(evt) {
console.log(evt);
}
// Enlever le listener
myButton.onClick = null;
// via addEventListener
myButton.addEventListener('click', myButtonClickHandler);
// Enlever le listener
myButton.removeEventListener('click', myButtonClickHandler);
```