` and be sure they're the ones we want.
```js
function enableAlignmentButtons() {
* // First, we get the wrapping div with the ID
const wrappingDiv = document.querySelector("#alignment-btns");
* // Then, we get all the buttons inside it
const buttons = `wrappingDiv`.querySelectorAll("button");
* // For each button in our div...
for (const button of buttons) {
* // ...we tell the browser to execute alignAllText once it is clicked
button.addEventListener("click", alignAllText)
}
}
```
> Call this function in the `document.onreadystatechange` callback function.
If you run this code now, you'll get a `ReferenceError` on the browser's console:
```
script.js:34 Uncaught ReferenceError: alignAllText is not defined
at enableAlignmentButtons (script.js:34)
at HTMLDocument.document.onreadystatechange (script.js:11)
```
> That's normal: we didn't write the `alignAllText()` function. Let's do that now.
---
#### Align each paragraph
.breadcrumbs[
JavaScript - DOM API >
Document Object Model (DOM) >
Example 2 - alignment buttons]
The callback to `addEventListener()` is always called with one argument, which is the event itself.
Our `alignAllText()` function should then take this event as a parameter. We will use it to detect which button was actually clicked:
```js
function alignAllText(event) {
* // currentTarget is the clicked button. The first child nodes is its icon
const icon = event.currentTarget.childNodes[0];
* // With classList we can detect the type of the icon, and deduce the alignment
let alignment;
if (icon.classList.contains("fa-align-left")) {
alignment = "left";
} else if (icon.classList.contains("fa-align-center")) {
alignment = "center";
} else if (icon.classList.contains("fa-align-right")) {
alignment = "right";
}
* // We get all the paragraphs of the document
const pColl = document.getElementsByTagName("p");
* // For each of them...
for (const p of pColl) {
* // We remove any text alignment class
p.classList.remove("text-left", "text-center", "text-right");
* // We add an alignment class using the previously deduced alignment
p.classList.add(\`text-${alignment}`);
}
}
```
---
### Exercice - up to you again!
.breadcrumbs[
JavaScript - DOM API >
Document Object Model (DOM)]
We want to play with the form and the table we have in the user interface.
The requirements are that when a user submits the form by clicking on the "Register" button, we:
- get the value of the First name and Last name fields ;
- add a new row to the Table at the bottom of the page with those values in the right columns (give the `#` and `Startship` column a value of `###`).
> Good luck! And to help you:
> 1. Add a listener either on the form's submit event or the button's click event ;
> 1. In the callback function, start by getting the values from the fields
> 1. Then create the required HTML node tree for a new table row, filling in the values in the cells
> Do not hesitate to update the HTML code by adding ids or classes if this helps you. But don't overdo it!
---
## Resources
.breadcrumbs[
JavaScript - DOM API]
You will find the final HTML file for this course [here][fef]
**Documentation**
- [DOM documentation][domdoc]
[domdoc]: https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model
[bootstrap]: ../bootstrap
[js-bas]: ../js
[course]: ../setup
[chrome]: https://www.google.com/chrome/
[vscode]: https://code.visualstudio.com/
[bsef]: https://gist.githubusercontent.com/Tazaf/18732ef01164f7b6348443c4c4748f42/raw/ecae62ab21eca302bcbfc0ffef8df1fd30d9e6af/index.html
[fef]: https://gist.githubusercontent.com/Tazaf/e7286440370e1c5197999b0da2e84f9e/raw/f1765006a58b7bff78c0dbe05739227734b23ebf/index.html
[text-content]: https://developer.mozilla.org/fr/docs/Web/API/Node/textContent
[a]: https://developer.mozilla.org/fr/docs/Web/HTML/Element/a