0

I'm quite new to jquery... I'm working with ToastMessge plugin (demo and download here)..

I'd like to pass name of Toastmessage function by reference to another function's argument but the following doesn't work :

$(document).ready(function() {
        function switchMsgType(fref) 
            {
             var fname = null;  
             fref(fname);
            }
            var alfa = $("#msgtype").html();

            switchMsgType(alfa);
         });

html:

<body>

    <div id="msgtype"><?php echo $_GET['msgtype']; ?></div>
    <div id="msg">bla bla ....</div>  <!-- txt of the message -->
    ...

The only working version that i have is this :

...
switchMsgType(showStickySuccessToast);
... 

but i must have sonething in the page to keep my PHP $_GET msgtype.... so i can switch from 'Warning' to 'Success', etc... I already tried with :

$.trim($("#msgtype").text());  or...    $.trim($("#msgtype").html());

and others.. but nothing... Thanks in advance... :)

lollo
  • 165
  • 1
  • 13

1 Answers1

0

Based on the link you provided it seems like your are just trying to invoke a different "Toast Message" based on the contents of your "msgtype" div, I don't see why you need to use a function pointer for that, you should be able to simply pass in the name of the type of toastMsg and use that to display the appropriate toast message,

For Example

function callToastMsg(msgType, msg) {
      $().toastmessage('show' + msgType + 'Toast', msg);
}

$(function() {
    var msgType = $('#msgtype').text();
    var msg = $('#msg').text();
    callToastMsg(msgType, msg);
}); 

HTML

<div id="msgtype"><?php echo $_GET['msgtype']; ?></div> //say $_GET['msgtype'] == "Success"
<div id="msg">bla bla ....</div>  <!-- txt of the message -->

If your trying to do something similar to the link you posted in your comment then you need to make sure that those functions exists, but I'm really not sure why you would want to do it this way.

For example

function success(msg) {
    $().toastmessage('showSuccessToast', msg);
}

function warning(msg) {
    $().toastmessage('showWarningToast', msg);
}
function callOtherFunction(nameOfFunction, msg) {
    window[nameOfFunction](msg);
}

$(function() {
    var nameOfFunction = $('#msgtype').text();
    var msg = $('#msg').text();
    callOtherFunction(nameOfFunction, msg);
}); 
Community
  • 1
  • 1
Jack
  • 10,943
  • 13
  • 50
  • 65
  • Oh.. your first solution seems to be good .. and it works, however i need to implement in the plugin the 'Sticky' way too.. I was very interested to know if you could work with function pointers in jquery.. Thank you very much – lollo Jul 27 '12 at 15:31
  • You definitely can work with function pointers in javascript, the second way I provided isn't really the way you would normally do it (since your just passing the name of the function as a string and relying on the fact that the function exists), I was just basing it off the SO question you linked to. – Jack Jul 27 '12 at 15:34
  • a string, evidently can not be passed as a parameter... i guess :) .. just to know .... i tried even to pass my var 'alfa' by reference... with bad results – lollo Jul 27 '12 at 15:47