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

addEventListener is not working

3
Hi,
This is my code

Expand|Select|Wrap|Line Numbers
  1. $("#<%=imgBtDone.ClientID %>").click(function(e) {
  2. var button = document.getElementById('imgBtDone');
  3.  
  4. button.addEventListener('click', function () {                       alert('test'); }, false);  
  5. });
here addEventListener not executing.

Thanks,
spv2
Jan 19 '13 #1
4 2423
Anas Mosaad
185 128KB
addEventListener is the standard method for attaching adding events to components. However, IE is not following the standard and has its own functions to attach events (i.e. attachEvent). It's recommended to develop your own functions to workaround cross browser compatibilities. I use this cross browser function to add event listeners:
Expand|Select|Wrap|Line Numbers
  1. function attachEventToComponent(component, eventName, eventHandler){
  2. if (component.addEventListener){
  3.    component.addEventListener(eventName,eventHandler, false);
  4. }else if (component.attachEvent){
  5.    try{
  6.        component.detachEvent("on"+eventName, eventHandler);
  7.    }catch(e){}
  8.    component.attachEvent("on"+eventName, eventHandler);
  9. }
  10. }
  11.  
Jan 19 '13 #2
Dormilich
8,658 Expert Mod 8TB
However, IE is not following the standard and has its own functions to attach events (i.e. attachEvent). It's recommended to develop your own functions to workaround cross browser compatibilities.
As of IE 9, addEventListener() is eventually supported.


I use this cross browser function to add event listeners:
you are aware that you can’t use this in you event handlers?
Jan 20 '13 #3
Anas Mosaad
185 128KB
Thanks Dormilich for the clarification. I honestly didn't about it as the function already supports it.
Regarding this, you can workaround this with closure.
Jan 20 '13 #4
Dormilich
8,658 Expert Mod 8TB
Regarding this, you can workaround this with closure.
workaround for a workaround, eh?

yea, cross-browser issues are a real bother. not even jQuery tackled the full IE event issue.
Jan 20 '13 #5

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

Similar topics

4
by: PJ | last post by:
Is it possible to extend the Node object so that the DOM function addEventListener can be used w/ IE? Does anyone have an example of this? Thanks, Paul
1
by: philjhanna | last post by:
Hi I'm having a problem applying return false to a div via addEventListener. I'm adding this so that I can drag (click-hold-move) over an image. (Its so that I can add zooming to the image)...
2
by: rhamlin | last post by:
I'm working on a class. When instantiating I pass the variable name referencing the class (vw) and the page object where the class will display (view). In this class I create a DIV. I also...
3
by: Jake Barnes | last post by:
37 Signals has built some awesome software with some features I wish I knew how to imitate. When I'm logged into my page (http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver I'm...
3
by: Arif Mohd | last post by:
i have given the stmt as document.addEventListener('onkeydown', my_onkeydown_handler, true); and the function defined was function my_onkeydown_handler() { alert('called');
10
by: Peter Michaux | last post by:
Hi, Today I have been testing the event models from Netscape 4.8 and IE 4 to the current crop of browsers. I'd like to write a small event library similar in purpose to the Yahoo! UI event...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
6
by: soulmach | last post by:
Using Firefox, I am trying to access a link within my HTML document, then use addEventListener() to add a click event to that link. I am using two external JS files. ajaxModal.js has the...
1
by: sylver | last post by:
Hi, Unregistering event listeners (or memory allocation) is a good practice in programming, but do we need to use removeEventListener() for all addEventListener() implementation? Please...
0
by: wpjoju | last post by:
i have this code which adds an event listener to a newly opened window but it doesn't seem to work in google chrome. the problem is window.addEventListener seem to work in chrome but if you do...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.