Genesis 38:6-10
6Judah got a wife for Er, his firstborn, and her name was Tamar.
7But Er, Judahs firstborn, was wicked in the Lords sight; so the Lord put him to death.
8Then Judah said to Onan, Lie with your brothers wife and fulfill your duty to her as a brother-in-law to produce offspring for your brother.
9But Onan knew that the offspring would not be his; so whenever he lay with his brothers wife,
he spilled his sperm on the ground to keep from producing offspring for his brother.
10What he did was wicked in the Lords sight; so he put him to death also.
It was not about the spilling of seed, it was about disobeying God's order to create for his brother, an offspring that was not wicked.