473,232 Members | 1,568 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,232 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 1148
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.