[thelist] DOM - adding a function to a dynamically added element...

Pavel Dudrenov dudrenov at gmail.com
Mon Apr 25 17:50:20 CDT 2005


Dude there are a few things you can do. But the main to are:

First don't use setAttribute cuz' that wont work on IE.

myEl.onclick = functionName  "NOTE you have to put the function name
as a variable without the (); or else the function will get executed
right there on the spot when the browser parses the code - so no () "

but if you do need to pass something to your function you would have
to use anon function like you suggested myEl.onclick = function ()
{functionName(this);} or you can use addEventListener/attachEvent

Hope this helps,
Pavel Dudrenov


On 4/25/05, Tom Dell'Aringa <pixelmech at yahoo.com> wrote:
> Hi there, I'm rusty and have forgotten the best way to do this.
> 
> I'm adding a link inside a DIV dynamically on my page using the DOM. This link needs to have a
> function tied to the onclick event handler. If I remember correctly, you cannot do this using
> .setAttribute -  because IE doesn't like it (based on my tests, that is what is happening)
> 
> I had thought then you added it using the property of the object:
> 
> myLink.onclick = functionName; ( with or without the brackets?)
> 
> But this doesn't seem to work. My problem is compounded by the fact that I want the link to return
> false: The link should be this:
> 
> <a href="" onclick="toggleHelp();return false">Page Help</a>
> 
> Do I have to write this as an anonymous(?) function like:
> 
> myLink.onclick = function { statements here }
> 
> I'm rusty on this stuff it has been awhile, I can't remember the correct way. I also don't see a
> way to add the return false, or maybe 'return toggleHelp()' then return false in the
> function...still how would I add the return? I need to call this function from this link...
> 
> Thanks
> 
> Tom
> 
> http://www.pixelmech.com/
> 
> Melissa: Ace, Where are you?
> Ace Ventura: I'm in Psychoville and Finkle's the Mayor.
> 
> --
> 
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
> 
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>


More information about the thelist mailing list