Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have got a javascript function which displays the notification

JavaScript
(function () {
      $('.demo-1').on('click', function (e) {
         e.preventDefault();
         $.notify('I am a alert box.');
       });
     })(jQuery);


What I have tried:

I want to pass dynamic values to it and for that, I've customized the above code as follows:

JavaScript
(function ShowNotification(message) {
      $.notify(message);
})(jQuery);


Javascript code is not working, can anyone help me fix this issue?

Thanks
Posted
Updated 14-Feb-17 21:37pm
v3

You do not need to create a IIFE here like you have in first example, just define a normal function here:

JavaScript
function ShowNotification(message) {
      $.notify(message);
});


It is not working because you are calling the function just after creation and passing jQuery as parameter to the function while you intend to pass message to the function from calling side.

You would have to call it from the click event function this way:

JavaScript
(function () {
      $('.demo-1').on('click', function (e) {
         e.preventDefault();
         ShowNotification('I am a alert box.');
       });
     })(jQuery);
 
Share this answer
 
v3
Comments
mimtiyaz 15-Feb-17 3:45am    
The code doesn't work without IIFE
Ehsan Sajjad 15-Feb-17 3:48am    
how are you calling it ?
mimtiyaz 15-Feb-17 4:00am    
onClientClick event of input control
Ehsan Sajjad 15-Feb-17 4:09am    
show the code by updating the question, how are you doing that?
mimtiyaz 15-Feb-17 4:53am    
<asp:Button id="btnSubmit" runat="server" onClientClick="ShowNotification('Saved Successfully','success');" Text="Submit">
try this

var message = 'I am a alert box.';

       $(function () {
           $('.demo-1').on('click', function (e) {
               e.preventDefault();
               $.notify(message);
           });
       });

       function showAndChangeNotification(msg) {
           message = msg;
           $.notify(message);

       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900