An electromagnet is a conductor which has a wire wrapped around it and
becomes a powerful magnet when electricity is passed through the wire.
If the electricity moves in one direction, the magnetic field becomes
southernly polarized, and if the electricity moves in the opposite direction,
the magnetic field becomes northernly polarized. If two electromagnets
have electricity moving in the same direction, then they will both create
the same direction of magnetic field, causing them to repel each other. If
the two magnets have their electricity moving in opposite directions, one
will become north and one will become south, causing them to attract
each other. The more electricity that passes through the conductor, the
stronger the magnetic field, and the less electricity, the weaker the
magnetic field.
How Electromagnets Work