d00rman
d00rman

Reputation: 330

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();

Upvotes: 2

Views: 314

Answers (1)

dfsq
dfsq

Reputation: 193261

.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>";

Upvotes: 1

Related Questions