Genesis 38:9

Amplified Bible (AMP)
Onan knew that the child (heir) would not be his [but his dead brother’s]; so whenever he lay with his brother’s widow, he spilled his seed on the ground [to prevent conception], so that he would not give a child to his brother.

Read Genesis 38:9 in other translations