469,319 Members | 2,329 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,319 developers. It's quick & easy.

create event -property

Ciary
247 Expert 100+
hi all,

i have another problem. in my website, i have an option to create divs dynamically. the problem is that i want to do something when you click on one of those divs. i thought this would do the trick
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("MyDiv").onClick = "MyFunction()"
unfortunatly, it doesn't. :(

does anyone know how it should be done?
Apr 9 '09 #1
5 1321
acoder
16,027 Expert Mod 8TB
Try:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("MyDiv").onclick = MyFunction;
Apr 9 '09 #2
Ciary
247 Expert 100+
ok, that works. but now i need to send an argument when i execute the function.

Expand|Select|Wrap|Line Numbers
  1. document.getElementById("MyDiv").onclick = MyFunction('foo');
this doesn't seem to work since it triggers the event when i create it but not when i click on the div.
Apr 9 '09 #3
acoder
16,027 Expert Mod 8TB
You need to set it to a function object rather than a function call which is why I removed the parenthesis earlier. Try this:
Expand|Select|Wrap|Line Numbers
  1. document.getElementById("MyDiv").onclick = function() {MyFunction('foo');}
Apr 9 '09 #4
Ciary
247 Expert 100+
acoder, you're the best. :)

ty so much
Apr 9 '09 #5
acoder
16,027 Expert Mod 8TB
You're welcome :) Glad to help.
Apr 9 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.