Q:

Autoclose alert doesn't alert

I cannot se why my autoclose doesn't work.
I use php to echo out javascript. I do echo both script. First the "not working" and then the "working". Only the "working" script shows up!
What's wrong?
Not working
echo "<div class='alert-message'></div>";
echo "<script type='text/javascript'>";
  echo "$('.alert-message').alert();";
  echo "window.setTimeout(function() { $('.alert-message').alert('close'); }, 5000);";
echo "</script>";

Works
//Echo succes
echo "<script type='text/javascript'>";
  echo "alert('Välkommen');";
  echo 'window.location = "page.php"';
echo "</script>";
die();

A:

.alert-message div is not yet loaded in DOM when you are using it. Try to echo it before script:
echo "<div class='alert-message'></div>";    
echo "<script type='text/javascript'>";
  echo "$('.alert-message').alert();";
  echo "window.setTimeout(function() { $('.alert-message').alert('close'); }, 5000);";
echo "</script>";

