469,325 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Dynamic 'on' handlers.

Markus
6,050 Expert 4TB
Is it possible to do something like the below (it should explain itself)

Expand|Select|Wrap|Line Numbers
  1. var event = 'onclick';
  2.  
  3. document.getElementById('id').event = alert("it works);
  4.  
May 2 '09 #1
7 1104
omerbutt
638 512MB
@Markus
Expand|Select|Wrap|Line Numbers
  1. var event1='onClick';
  2.     document.getElementById('but').event1= alert("it works");
  3.  
this would work only on page loading and for sure it will show the alert but you cannot assign an event like this to any button or form element ,means if you are trying this way to attach the event that should occur when we will later click that button or form element , that will not work for you
regards,
Omer Aslam
May 2 '09 #2
acoder
16,027 Expert Mod 8TB
@Markus
Yes, except that the syntax would be something like:
Expand|Select|Wrap|Line Numbers
  1. var anevent = 'onclick';
  2. document.getElementById('id')[anevent] = function() {alert("it works");}
  3.  
Changes:
1. changed event to anevent. IE seems to have problems with setting variables with the name 'event' (messes with the global window.event).
2. Use bracket notation for strings.
3. Use a function object, otherwise it would alert "it works" as soon as this event is attached.
4. You forgot to close the string ;)

Note that you could alternatively use addEventListener/attachEvent (look them up).
May 2 '09 #3
acoder
16,027 Expert Mod 8TB
@omerbutt
If you use a function object, you can. Also note that JavaScript is case-sensitive, so it should be "onclick", not "onClick".
May 2 '09 #4
omerbutt
638 512MB
@acoder
hey but when using it on the button type the property is named as "onClick" rather than "onclick" isnt it :( or i am missing something
May 2 '09 #5
acoder
16,027 Expert Mod 8TB
You mean within the HTML code?
May 2 '09 #6
omerbutt
638 512MB
@acoder
yes wont that effect changing the spelling for the same property that we are setting through javascript
regards,
Omer Aslam
May 2 '09 #7
acoder
16,027 Expert Mod 8TB
No, in JavaScript they must be lower-case. With HTML, you can get away with using mixed case, but XHTML is more strict. In other words, it should be lower-case in HTML too. See, for example, http://www.w3.org/TR/REC-html40/inte....html#h-18.2.3.
May 2 '09 #8

Post your reply

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

Similar topics

reply views Thread by Kenton Smeltzer | last post: by
3 posts views Thread by Leo J. Hart IV | last post: by
2 posts views Thread by Mark Broadbent | last post: by
3 posts views Thread by Kris Palmer | last post: by
2 posts views Thread by pamelafluente | last post: by
3 posts views Thread by =?Utf-8?B?ZWFndWlsYXI=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.