In this paper we propose a method for online motion planning of constrained nonlinear systems. The method consists of three steps: the offline generation of a library of parametric trajectories via direct trajectory optimization, the online search in the library for the best candidate solution to the optimal control problem we aim to solve, and the online refinement of this trajectory. The last phase of this process takes advantage of a sensitivity-like analysis and guarantees to comply with the first-order approximation of the constraints even in case of active set changes. Efficiency of the trajectory generation process is discussed and a valid strategy to minimize online computations is proposed; together with this, an effective procedure for searching the candidate trajectory is also presented. As a case study, we examine optimal control of a planar soft manipulator performing a pick-and-place task: through simulations and experiments, we show how crucial online computation times are to achieve considerable energy savings in the presence of variability of the task to perform.