Traditionally the Norwegian kings had been elected by the several things held around the country. This practise often led to there being several kings at the same time. After the civil wars in the 12th century, Erling Skakke introduced a new law that there would be only one king, and that the oldest surviving son of the previous king should inherit the throne. At the same time, Erling introduced a system where the most powerful men in the country would meet with the power to veto a candidate if they found him to be unsuitable.
Erling's son Magnus V had no heirs.
List of heirs to the Norwegian throne Wikipedia
During the reign of Inge II there was never any clear heir, and after his death several relatives claime the throne. At a church meeting in 1223 it was decided that the church was backing the candidacy of Håkon Håkonsson. There was no heir until his first son was born in 1226.Hereditary Kingdom of Norway#Unions with Denmark and Sweden
Norway became an elected kingdom, and joined several unions with Sweden and Denmark. However, the tradition arose that during his own lifetime the King would choose an heir among his sons and have him hailed as such, strongly influencing the electing council's choice.
In 1665 Frederick III signed a new law, proclaiming the kingdom to be hereditary.