473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 1150
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

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

Similar topics

0
by: Kenton Smeltzer | last post by:
Hello All, I am having a problem with events and the addition of controls on a page I am developing. First let me tell you what I have tried and then maybe someone can see something I missed. ...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
2
by: Mark Broadbent | last post by:
I had a requirement to code dynamic delegates, so they attach detach from the button as needed ... then after time realised that the button is not retaining it's click handlers. To circumvent this...
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
3
by: Kris Palmer | last post by:
hi, can somebody explain this problem? it's driving me crazy! i have a requirement to dynamically create a variable quantity of timers with associated start button based on the contents of a...
2
by: pamelafluente | last post by:
Hi guys, I am trying to add some code in the body of a document just after it has loaded. I have the following code (this is referenced in an html page). When I click on the document I...
3
by: =?Utf-8?B?ZWFndWlsYXI=?= | last post by:
Hi, I am trying to dynamically generate a menu, based on entries on a text or xml file. The text file contains the "tree" after which the menu will need to be created. Something like the...
3
balabaster
by: balabaster | last post by:
I've got a user control that builds a table of dynamic data based on a :LINQ class holding the data. The data is loaded using the LoadData(DataInstance) method. The table it builds contains a...
4
by: Meganutter | last post by:
Hello all, im making a dynamic .NET page which reads downloads from a XML file. there are two controls that have handlers, a category switcher and a download streamer (so people cant surf...
1
anfetienne
by: anfetienne | last post by:
i have this code below that i made....it loads vars from txt file splits it then puts it into an array....once in an array it the brings the pics in from the array to create thumbnails and a larger...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.