Fitness is a group measurement function
Things which appear to be good for the individual are not necessarily selected for, like selfishness (which benefits you by harming those around you). Things which appear bad for the individual are not necessarily selected against, like altruism (the reverse): Getting yourself killed is (obviously) not good for you. So why did this behavior develop? Why would evolution keep it around? The reason is evolution isn't concerned with you. It doesn't even care about you. It cares about the population.
Danger weeds out the weak
Sure, this is a very noisy selection mechanism. Certain kinds of danger will kill anybody. But many kinds of danger will kill weak or stupid elves much more readily than more resilient individuals.
Accordingly, a genetic predisposition to hiding behind mommy will tend to allowmake it easier for less fit elves to survive long enough to spread their genes. In contrast, a predilection for adventurous mischief will tend to get less hardy or intelligent elves killed at a higher rate.
In other words, this gene is bad for individuals but good for the population because it checks the spread of, and indeed works to eliminate, other genes which are harmful.