How to attach onclick event for a JavaScript generated textbox?
Taking your example, I think you want to do this:
el.onclick = function() { displayDatePicker(el.id); };
The only trick is to realise why you need to wrap your displayDatePicker
call in the function() { ... }
code. Basically, you need to assign a function to the onclick
property, however in order to do this you can't just do el.onclick = displayDatePicker(el.id)
since this would tell javascript to execute the displayDatePicker
function and assign the result to the onclick
handler rather than assigning the function call itself. So to get around this you create an anonymous function that in turn calls your displayDatePicker
. Hope that helps.
Like this:
var el = document.createElement('input');
...
el.onclick = function() {
displayDatePicker('attendanceDateadd1');
};
BTW: Be careful with case sensitivity in the DOM. It's "onclick"
, not "onClick"
.