Rhino beetle on tree

Debuggin’ the DOM Tree

In the past, I have written about ways to debug, which, in most cases, incorporates using the inspection tool built into modern browsers. That is definitely a cool tool and the one I use the most. With that said, I want to talk a bit today about debugging the DOM and how you can even use the DOM to debug itself.

With the HTML DOM, it does not take long to get things complex. We need to be able to dissect…or slice and dice…things up to fix any bugs. You have seen the usage of alerts, the console, document.writes and breakpoints. Here is another tip…

Utilize the DOM itself to tell you what is happening…

createElement() can be used within the DOM to ‘manufacture’ HTML code and then use appendChild to add to it. So what would this look like? Imagine you have an application that changes as the user clicks on buttons, but it is not functioning as intended. You could put an empty div on your page…

You can then use this div to write content to the page to observe what is happening within the DOM code you construct. Imagine you have a function running and you have code in there that is flow control code (if statements, switch statements, etc.). Within this code, you are attempting to alter the DOM. You can write a function that will record the information to your history div right on your page.

All this code does is pull information from the flow control code and dynamically create it and stick it into the history div.

Happy Coding!

Clay Hess

Share this post