An electronic device includes a casing, a stylus, a first magnet, a magnetic ring, and a pair of second magnets. The casing has a surface and an accommodation groove recessed from the surface. The stylus has a front end and a rear end. The first magnet is disposed at one of the front end and the rear end of the stylus, and the magnetic ring is disposed at the other of the front end and the rear end of the stylus. The second magnets are disposed in the casing and located below the accommodation groove, where locations of the first magnet and the magnetic ring correspond to locations of the second magnets.