2

I have made a page, where if the user update its form, the backend will send the user back to the form with a message like this:

header('loaction:/index.php?msg');

On the index.php site, it loads this:

if(isset[$_GET['msg'] {
$message = 'Updated';
echo '<div class="update_message">'.$message.'</div>';
};

This works fine, but I would like the message to fade out after a couple of seconds. I tried this, but wouldn't fade out:

if(isset[$_GET['msg'] {
$message = 'Updated';
echo '<div class="update_message">'.$message.'</div>';
echo "<script type='text/javascript'>
$(function() {
$('div.update_message').hide().fadeIn().delay(3000).fadeOut('slow');
})
</script>";
    }; 

The message still apears but it doesn't fade out.

1

You're using a jQuery snippet but have you actually loaded the jQuery library?

You can do do by using: JHtml::_('jquery.framework');

Here is the updated code with some modifications:

$msg = JFactory::getApplication()->input->get('msg');

if (isset($msg))
{
    echo '<div id="update_message">Updated</div>';

    JHtml::_('jquery.framework');
    JFactory::getDocument()->addScriptDeclaration('
        jQuery(function($) {
            $('#update_message').delay(3000).fadeOut('slow');
        });
    ');
}

Hope this helps

  • Should this still be inside a <script> ? Haven't worked with jQuery ever :D – Daniel Jensen Jun 15 '18 at 9:30
  • Arh I made it work :) Only thing I needed to edit, was the " instead of ' after addScriptDeclaration( and again at last :) – Daniel Jensen Jun 15 '18 at 9:34
  • No need to use a <script> tag here as the addScriptDeclaration function will automatically insert it inside that tag for you ;) – Lodder Jun 15 '18 at 10:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.