I have a dynamic HTML element and I want to attach a function to the event handler.
drilldownTextInput = $("<input type='text' class='drilldown-search' data-drilldown-options='" + drilldownOptions + "' />");
drilldownTextInput.on('click', showOptions());
function showOptions() {
console.log("Working.");
}
The problem is that on page load the showOptions()
function is called immediately and not on the click event.
If I do this, the event works as expected - only on click.
drilldownTextInput = $("<input type='text' class='drilldown-search' data-drilldown-options='" + drilldownOptions + "' />");
drilldownTextInput.on('click', function() {
console.log("Only appears on click...");
});
What is the difference between the two and how can I invoke a function in order to keep things DRY?