To factor $ x^2+3x+kx+3k $ we can use factoring by grouping.
Group $ \color{blue}{ x^2 }$ with $ \color{blue}{ 3x }$ and $ \color{red}{ kx }$ with $ \color{red}{ 3k }$ then factor each group.
$$ \begin{aligned} x^2+3x+kx+3k &= ( \color{blue}{ x^2+3x } ) + ( \color{red}{ kx+3k }) = \\ &= \color{blue}{ x( x+3 )} + \color{red}{ k( x+3 ) } = \\ &= (x+k)(x+3) \end{aligned} $$