471,887 Members | 1,255 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,887 software developers and data experts.

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 1354
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 Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.