I found the Answer in this SO post:
Rather than do all this by hand, the browser has methods to tell you if the current selection or caret is bold, italic or whatever:
document.queryCommandState()
(MSDN), for binary commands such as a bold and italic, anddocument.queryCommandValue()
(MSDN) for commands with a value, such as font-related commands.