An electronic device includes a casing, a stylus, a first magnet, and a pair of second magnets. The casing has a surface and an accommodation groove recessed from the surface. The accommodation groove has an accommodation portion parallel to the surface and a pair of connection portions obliquely connected between the surface and the accommodation portion. The stylus has a front end and a rear end, and the first magnet is disposed at the rear end. The second magnets are disposed in the casing and located below the accommodation portion.