Placed context outside of loop

$actions was inside an if else and therefore not accessible to
appendChild elements.