Yes you can leave the onion juice in your hair overnight but I would wear a shower cap to prevent the onion juice from getting onto pillows or bedding.
You can also just apply the lemon juice to your hair and leave the lemon juice in your hair for 2 to 3 hours during the day and then rinse the lemon juice out of your hair.
Both onion juice and castor oil can both help strengthen your hair and roots.