I'm trying to attach a click event to a button that runs calls from both a parameter and from itself.
What I want is a 1st alert to always show, then execute the 2nd alert that is stored in myparam.onclick as a string.
Non-working attempt:
var myparam = { onclick: 'alert("second");' };
$('#my_button_id').on('click', function(){ alert('first'); myparam.onclick });
I know that if I change myparam to a legitimate function, such as:
var myparam = {onclick: function(){alert('second');} }
I can call that just fine by
$('#my_button_id').on('click', myparam.onclick);
but I still don't know how to concatenate two together. I don't want to call the first alert every time in the myparam func call.
Any help is appreciated.
eval()on that string but that doesn't come without big security concerns. An alternative is to store named functions as strings and params as other property and then call named function from those like ...myFunctions[myparam.onclick](myparam.clickParams)data-attributes also. Look at how whole bootstrap library works for example