You've already kinda answered your own question.
First you would check which enemy is selected, and store that in a variable. Then you would check the distance between the player and the enemy is withing striking distance. And if so, start playing a loop of the attack animation, and apply damage every X seconds to the selected target.