Google actually does read the DOM, but not every search engine is made equally. But it also doesn’t capture the nofollow specifically, because it’s created too late in the DOM. Google will catch the link first.
Using JS for similar purposes may work well, such as adding a class to your links, but definitely not for nofollowing. It will provide you with zero functionality with many search engines. I’ve seen it happen more than once, which is why I’m putting up this quick notice.
Better yet, just add nofollow to your links manually or, if you’re using something like CKEditor, set it so that external links automatically get the nofollow relationship.