We demonstrate that it is possible to manipulate the magnetic coupling between two nanomagnets by means of an ac electric field. In the scheme suggested, the magnetic coupling is mediated by a magnetic particle that is in contact with both nanomagnets via tunnel barriers. The time-dependent electric field is applied so that the height of first one barrier then the other is suppressed in an alternating fashion. We show that the result is a pumping of magnetization from one nanomagnet to the other through the mediating particle. The dynamics of the magnetization of the mediating particle allows the coupling to be switched between being ferromagnetic and being antiferromagnetic.