473,379 Members | 1,170 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,379 software developers and data experts.

Adding event listener

Claus Mygind
571 512MB
The event listeners I am adding do not seem to function when the event occurs. Using the inline javaScript method the events work fine (see below).

Expand|Select|Wrap|Line Numbers
  1. <input type = "text" id = "ID" 
  2.   onKeyPress="this.value = this.value.toUpperCase(); return isId(event, this);"
  3.   onChange="requestClient(); DataChanged()"
  4. />
  5.  
I want to add the event listeners as suggested by Dormilich. Which with the help of Dormilich, I was able to do on a form I create on the fly. See http://bytes.com/topic/javascript/an...ll#post3657732.

Now I am trying to add more to other controls on my form. Here is my code.

Here is the basic form with same control example as above. I want to hook the two event listeners (see code above) to the "ID" input box. The onload event handler for the body works fine and fires after the page has loaded.
Expand|Select|Wrap|Line Numbers
  1. <head>
  2.  <script language="JavaScript">
  3.  <!--
  4.   function myStartUp(obj){
  5.     assignEventListeners();
  6.   }
  7. //-->
  8. </script>
  9.  
  10. </head>
  11. <body onLoad = "myStartUp(this);">
  12. ... other code ...
  13.  <input type = "text" id = "ID" />
  14. ... other code ...
  15. </body>
  16.  
Here is how I create the event listeners in my .js library
Expand|Select|Wrap|Line Numbers
  1. //global object
  2. var g = {};
  3.  
  4. function assignEventListeners(){
  5.     g.getClient = document.getElementById('ID');
  6.     if (g.getClient.addEventListener)
  7.     {
  8.             g.getClient.addEventListener("onChange",function(){requestClient();},false);
  9.     }
  10.  
  11.     g.setToUpper = document.getElementById('ID');
  12.     if (g.setToUpper.addEventListener)
  13.     {
  14.         g.setToUpper.addEventListener("onKeyPress",
  15.                                         function()
  16.                                         {
  17.                                             this.value = this.value.toUpperCase(); 
  18.                                             return isId(event, this);
  19.                                         },
  20.                                     false)
  21.     }
  22.     return;
  23. }
  24.  
References to the other functions in the eventListeners are the same in both the inline javaScript and eventListener so I know they function fine.

So why does the event not fire when they happen?
Apr 26 '11 #1

✓ answered by Claus Mygind

So I think I have the answer, but I sure would like to find the documentation on it so I can see a complete list.

In moving the inline javaScript from HTML tags into an event listener I have to change the name of the event. For example:

"onClick" becomes "click"
"onChange" becomes "change"

that becomes:
Expand|Select|Wrap|Line Numbers
  1. onKeyPress="this.value = this.value.toUpperCase(); return isId(event, this);"
  2.  
this
Expand|Select|Wrap|Line Numbers
  1.     g.setToUpper = document.getElementById('ID');
  2.     if (g.setToUpper.addEventListener)
  3.     {
  4.         g.setToUpper.addEventListener("keypress",
  5.                                         function(evt)
  6.                                         {
  7.                                             g.setToUpper.value = g.setToUpper.value.toUpperCase(); 
  8.                                             return isId(evt, g.setToUpper);
  9.                                         },
  10.                                     false)
  11.     }
  12.  

7 5609
Claus Mygind
571 512MB
So I think I have the answer, but I sure would like to find the documentation on it so I can see a complete list.

In moving the inline javaScript from HTML tags into an event listener I have to change the name of the event. For example:

"onClick" becomes "click"
"onChange" becomes "change"

that becomes:
Expand|Select|Wrap|Line Numbers
  1. onKeyPress="this.value = this.value.toUpperCase(); return isId(event, this);"
  2.  
this
Expand|Select|Wrap|Line Numbers
  1.     g.setToUpper = document.getElementById('ID');
  2.     if (g.setToUpper.addEventListener)
  3.     {
  4.         g.setToUpper.addEventListener("keypress",
  5.                                         function(evt)
  6.                                         {
  7.                                             g.setToUpper.value = g.setToUpper.value.toUpperCase(); 
  8.                                             return isId(evt, g.setToUpper);
  9.                                         },
  10.                                     false)
  11.     }
  12.  
Apr 26 '11 #2
JKing
1,206 Expert 1GB
The difference is that one is an HTML attribute and the other is a javascript event type.

Have a look: DOM Events
Apr 26 '11 #3
Dormilich
8,658 Expert Mod 8TB
I’m not sure why you need that global variable …

Expand|Select|Wrap|Line Numbers
  1. var el = document.getElementById('ID');
  2. // serve as XHTML and addEventListener() will work
  3. el.addEventListener("keypress", function (evt) {
  4.     this.value = this.value.toUpperCase();
  5. }, false);
  6. // isId() needs to use "this"
  7. el.addEventListener("keypress", isId, false);
one of the important points is that you can attach multiple functions to an element without putting it all in one function.

PS. Event Listeners don’t return either, you have to use evt.preventDefault()
Apr 27 '11 #4
Claus Mygind
571 512MB
Thanks that is just what I was looking for.
May 2 '11 #5
Claus Mygind
571 512MB
Thanks for the additional tip. I know I don't need the one global variable/object. It just gives me one single place to examine the information I have created. I read about it in a thread I came across on the web and it seemed like a good idea. I find it useful that's all
May 2 '11 #6
Dormilich
8,658 Expert Mod 8TB
the problem with globals is that they can be edited (or deleted) everywhere. a forgotten var can thus cause havoc.
May 2 '11 #7
Claus Mygind
571 512MB
Thanks you make a good point. Another area where I need to expand my programming skills.
May 6 '11 #8

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

Similar topics

2
by: Neil Cherry | last post by:
I have to modifiy a program which uses a serial interface and event listeners. The modification entails adding support for connecting to a terminal server via an IP address and port number instead...
6
by: Jeremy | last post by:
I want each instance of an object to be able to listen for input events. When the event occurs, a method of the object should be called, such that "this" is in scope and refers to the object...
6
by: Daz | last post by:
Hi, I am trying to find an event listener which will trigger when the pages starts loading, as opposed to when it's finished, or when the DOM content has finished loading. I am sure that such a...
6
by: Daz | last post by:
Hello everyone, I would like to open a child window from the parent, and add an onload event listener to the child window which will tell the parent when the document has loaded. As far as I...
0
daJunkCollector
by: daJunkCollector | last post by:
I wrote the following script. I have a movieclip named countyA_mc. I want the user to mouse over this movie clip, and result in the sending of a variable to the middle tier. I can establish...
1
Cristian Pinheiro
by: Cristian Pinheiro | last post by:
Hello guys, I was playing with Image Thumbnail Viewer (ITV) and found one problem, see http://www.dynamicdrive.com/dynamicindex4/thumbnail.htm for more details and how it works. Now the...
4
by: xend | last post by:
Hi. I want to add an event handler inside a function that will be used has an object constructor. The event handler to be added is an "method" of the object. if i do this: function...
3
bugboy
by: bugboy | last post by:
i'm trying to avoid inline event handlers by creating a listener. What i'm trying is based on reading this: http://www.quirksmode.org/js/events_advanced.html I can't figure out what i'm doing...
5
by: anagiga | last post by:
I am developing a page in html. I have a div which is playing a flash video. After the flash video stops, I want to take the user to a different url in the same window. I tried this function but it...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.