If you use the Vertical tab template when you're building your module, it's automatic. Key is you have to set your structure up the same way they have it (recommended) otherwise the OOTB functionality will not work. Should not have to write any code. Double check your steps, most likely missed one.