Or at least it would all work that way if we figured out the magnetic fields correctly.
To check, let’s use Lenz’s Right Hand Rule to see if the electrical circuit does indeed produce the magnetic fields we’ve drawn.
This rule says that if the right hand’s thumb points along the direction of current flow, the magnetic field lines will loop in the direction of the curled fingers.
Here the current flows out of the top positive battery terminal, and rightward along the top spring contact.
If we follow this current up into the top of the electromagnet, we see that the magnetic field lines do indeed tuck into its core.