At that time of the society husband is the sole owner of wife and children and he can give them away to anyone at will. To become a Buddha one needs to cultivate Dana Parami to the highest level. That includes the ability to give away anything you own. King Vessantara did not give away the children to be tortured. It was Jūjaka who decided to hit the children. So the Karmic consequences fall on Jūjaka. Not king Vessantara .
It was extremely tormenting for king Vessantara as you can see in the following passage:
What follow are the verses of the Great Being's lamentation.
"O when at morning or at eve for food my children cry, Opprest by
hunger or by thirst, who will their want supply?  How will their
little trembling feet along the roadway go, Unshod? who'll take them
by the hand and lead them gently so?
How could the brahmin feel no shame, while I was standing by, To
strike my harmless innocents? a shameless man say I!
No man with any sense of shame would treat another so, Were it a
servant of my slave, and I brought very low.
I cannot see him, but he scolds and beats my children dear, While like
a fish caught in a trap I'm standing helpless here."
These thoughts came into the Great Being's mind, through his affection
for the children; he could not away with the pain to think how the
brahmin cruelly beat his children, and he resolved to go in chase of
the man, and kill him, and to bring the children back. But no, he
thought: that was a mistake; to give a gift, then to repent because
the children's trouble would be very great, that was not the way of
the righteous. And the two following stanzas contain the reflexions
which throw light on that matter.
"He bound his sword upon his left, he armed him with his bow; I'll
bring my children back again; to lose them is great woe. But even if
my children die ’tis wicked to feel pain 1: Who knows the customs of
the good, yet asks a gift again?........"
Also read the passage where the son defends his father's actions
But the boy hearing this, could not stomach his father's blame; but as
though raising with his arm Mount Sineru smitten by the windblast 1,
he recited this stanza:
"How, grandsire, can he give, when none in his possession are, Slaves
male or female, elephants, a horse, a mule, a car?" The king said:
 "Children, I praise your father's gift: no word of blame I say.
But then how was it with his heart when he gave you away?" The lad
"All full of trouble was his heart, and it burned hot as well, His
eyes were red like Rohinī, and down the teardrops fell."
So you can see that he gave them away not because he didn't care, but it was a bond that he had to overcome. What's ironic is that they say that the Mara could trouble the Buddha so much under the Bodhi tree because king Vessanatara lamented a lot for this great gift he gave. In comparison, when Maithree Bodhisatta becomes the Buddha, Mara can only watch from afar. He cannot come close enough to trouble the Buddha.