John Brunner
John Brunner

Reputation: 2872

Noty (notifier) is not working inside a function

I have a misterious problem with the noty plugin click. When I create a notifier as explained on the site everything works fine, but when I put the same statement into a function which is called at the "onClick" event from a button, it doesn't work. There is no noty-notification coming, no errors, nothing happens if I press the button. When I remove the noty-thing and add a normal "alert" statement everything works fine. Is there something I have missed or is this a bug?

The original code looks like:

<head>
<script type="text/javascript" src="ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript" src="js/noty/jquery.noty.js"></script>
<script type="text/javascript" src="js/noty/layouts/top.js"></script>
<script type="text/javascript" src="js/noty/themes/default.js"></script>
</head>
...
<script>
var noty = noty({text: 'noty - a jquery notification library!'});
</script>

When I put the same statement into following function, noty don't works anymore.

<head>
<script type="text/javascript" src="ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript" src="js/noty/jquery.noty.js"></script>
<script type="text/javascript" src="js/noty/layouts/top.js"></script>
<script type="text/javascript" src="js/noty/themes/default.js"></script>
</head>
...
<a href="#" onclick="test_function(); return false;" class="btn">klick</a>
...
<script>
function test_function(){
   var noty = noty({text: 'noty - a jquery notification library!'});
}
</script>

Even if I change the var noty to var n (or something else) nothing changes, as stated in one stackoverflow question before.

Thanks in advance. Regards, john.

Upvotes: 1

Views: 3943

Answers (1)

Ernesto Morales
Ernesto Morales

Reputation: 41

it would be better if you use something like this:

<head>
<script type="text/javascript" src="ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript" src="js/noty/jquery.noty.js"></script>
<script type="text/javascript" src="js/noty/layouts/top.js"></script>
<script type="text/javascript" src="js/noty/themes/default.js"></script>
</head>

<script>
$("#element").click(function(){
   var n = noty({text: 'noty - a jquery notification library!'});
});
</script>

<a id="element" href="#" onclick="return false;" class="btn">klick</a>

And of course use an "id" on the element you wish to bind to noty. It will work perfectly.

Check out working example here: JSFiddle

Upvotes: 3

Related Questions