Make a demo using jsFiddle, since we cannot guess exactly what code you wrote!
Nobody uses onclick any more. onclick parties like it's 1999! Throw whatever book told you to do that in the trash. (Don't give it to charity, as it will lead somebody else astray...) Most would use a bit of jQuery code with a call to .on().
But if you insist on using onclick anyway, did you forget to add return false to the embedded JS in your onclick string?