Click to See Complete Forum and Search --> : Event handler to a HTML object (dinamically created)


micú
07-08-2003, 10:11 AM
How to register an event handler to a HTML object (dinamically created with javascript), and make it work with Internet Explorer?

The next crossbrowser statement creates dinamically a TextArea

-> var input = document.createElement("textarea");

Now, to register an event handler to this object could be done as follows:

-> input.setAttribute("onClick","alert('Hello');");

But THE PROBLEM is that the previous command only works with Netscape. It doesn't work properly with IE. I've tried doing:

-> input.onclick = "alert('Hello');";

and

-> input.onclick = clickHandle;
function clickHandle {
alert("Hello");
}

.. but it's still not working with IE

Thanks

Vladdy
07-08-2003, 11:07 AM
Correct way is to use
element.addEventListener
for IE use element .attachEvent.

You can write a cross browser function:

function installEventHandler(element,event,myFunction)
{ if(element.attachEvent) /* do IE thing */
else /* do DOM thing */
}

Charles
07-08-2003, 11:45 AM
I believe that it is also correct to simply assign a function to the handler.

input.onclick = function () {alert('I\\'ve ben clicked!')}