There has to be a simpler Javascript way to do this, but I don't know it.