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

How to add addEventListener in .js file with function in .aspx page?

Hi all,
I want to add listener to a div element from .js file. All the controls are being created in the .js file and then loaded in the .aspx page. The problem is that I cannot use jquery or prototype to add observe event to the div control from my .aspx page. [This is due to the nature of the solution itself. Any such attempt leads to error].

All I want is: the function defined in the .js file in the div's addEventListener should be a function in my .aspx itself, so that when the div is clicked in the page, it looks for the function in the page and run it.

Is this possible? Or is there any other option to do this?


Thanks in advance.
Mar 20 '09 #1
6 6337
rnd me
427 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1.     function addEvent(a, b, c) { // (object, event name, function)
  2.     return a.attachEvent ? 
  3.         a.attachEvent("on" + b, c) : 
  4.         (a.addEventListener ? 
  5.             a.addEventListener(b, c, false) : 
  6.             a["on" + b] = function (e) {e = e || window.event;c.call(a, e);}
  7.             );
  8.     }
  9.  
Mar 21 '09 #2
Hi,
Thanks for the reply. I tried to use this function in my js file this way:

div.addEventListener=addEvent(div,"click","appendS ignature()");

I suppose "appendSignature()" as a function in my .aspx page.

Here comes an error:

...Could not convert JavaScript argument...
a.addEventListener(b, c, false) :


I'm not quite sure if I am calling the function correctly. Could you give an example, please?

Thanks again.
Mar 23 '09 #3
Dormilich
8,658 Expert Mod 8TB
well, two things. you shouldn't redefine the addEventListener method (what sense would have this) and you use addEvent like
Expand|Select|Wrap|Line Numbers
  1. addEvent(div,"click",appendSignature);
of course, appendSignature() has to be a valid JS function...
Mar 23 '09 #4
Frinavale
9,735 Expert Mod 8TB
Sangam,

Is your <div> element an ASP.NET Panel?
If so you could just add the onclick to the Panel in your .NET code:

Expand|Select|Wrap|Line Numbers
  1. myPanel.Attributes.Add("onclick","appendSignature()")
Is your web application Ajax enabled?
If so then you have the .NET Ajax framework at your finger tips and you could use the $addHandler method to specify which method should be called when the click event occurs:
Expand|Select|Wrap|Line Numbers
  1.   var myPanelOrDivID = "<%= myPanel.ClientID %>";
  2.   $addHandler($get(myPanelOrDivID), 'click', appendSignature);


What are you trying to accomplish?
Mar 24 '09 #5
Hi,
Thanks all for the answers.
I changed appendSignature() to appendSignature only, and it worked!

Further, with addEvent, I do use "onclick", whereas with addEventListener, I do use "click" only.

Thanks again!
Mar 26 '09 #6
Dormilich
8,658 Expert Mod 8TB
@sangam56
anyways, there are different versions of addEvent() out there, so it depends on the version to use "onclick" or "click" (should be mentioned in the documentation)
Mar 26 '09 #7

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...
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...
1
by: mebemikeyc | last post by:
With Element.addEventListener, you can easily wire-up several different event handlers on the fly. This works great, but debugging can be difficult if I don't know what exactly is reacting to an...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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
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...
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...

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.