How does electromagnet works?
If a wire is wrapped around a ferromagnetic material (those which are attracted by a magnet) and a current is flown through the wire, the material behaves like a magnet. This phenomenon is known as electromagnetism. The electromagnet can attract other ferromagnetic material just like any magnet. Usually a soft iron-core is used for good results. By such a phenomenon you get a temporary magnet whose magnetic property can be switched on or off by a switch! You can try this at home by wrapping a wire around an iron-nail and using a battery for current.