In the electrolysis of aqueous solutions, the water molecules dissociate producing H+ and OH– ions:
H2O ⇌ H+ + OH–
Negatively charged OH– ions and non-metal ions are attracted to the positive electrode
If halide ions and OH- are present then the halide ion is discharged at the anode, loses electrons and forms a halogen
If no halide ions are present, then OH- is discharged at the anode, loses electrons and forms oxygen
Positively charged H+ and metal ions are attracted to the negative electrode but only one will gain electrons
If the metal is above hydrogen in the reactivity series, then hydrogen will be produced and bubbling will be seen at the cathode
This is because the more reactive ions will remain in solution, causing the least reactive ion to be discharged
Therefore at the cathode, hydrogen gas will be produced unless the positive ions from the ionic compound are less reactive than hydrogen, in which case the metal is produced