I have a function that I use as a callback for a custom event:
function checkNCreateTb() {
var tbName = $(this).attr('href').replace('content','orders')
/*...*/
}
I used .on() to handle the event that will callback that function
$('#group-pills a').on('shown', checkNCreateTb)
I need to apply the same function the first time the user clicks a button in another part of the DOM. I solved it attaching a click event to the button and triggering the shown event. However, I think this looks very ugly, Is there a better way to get the same result?
The code:
$('button#action-button').on('click',function() {
$('#group-pills a:first').trigger('shown')
$(this).off('shown') //removing the handler
})
//Create the DataTable if it wasn't already created
$('#group-pills a').on('shown', checkNCreateTb)
I would use
$.proxyto create a function reference withthisset to a particular value:Or, alternatively, use
callto set the value ofthisincheckNCreateTb:As a side note, you do not need to prefix your
#idselector with a tag name, sinceidis as specific as you can get and jQuery will usedocument.getElementbyIdunder the hood.