Search code examples
javascriptjqueryhtmljgrowl

jGrowl take message from html


Im trying to make jGrowl work with HML in a way, that I have messages in HTML and I want jGrowl display them as jGrowl div. Ie.: I try to upload a file, but it fails, so I show a message:

<div class="message">File upload failed!</div>

What I want, is jGrowl to search for those messages, hide them on page and display them using itself.

Cos the way I would have to do it, is to edit all messages to this:

$(document).ready(function(){
    $.jGrowl(
        "File upload failed!",{
        life: 5000
    });
});

The problem is, if somebody has JS disabled, he wont see anything and also, code is longer.

I hope you understand what I want.

Thank you for replies. Here is working code:

$('.message').each(function(){
    var $this = this;
    $(this).hide();
    $.jGrowl($(this).html(), {life: 5000});
});

Solution

  • Try this code:

    $('.message').each(function(){
      var $this = $(this);
      $this.hide();
      $.jGrowl($this.text(), life: 5000);
    });