473,586 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

68 New Member
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 addEventListene r 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 6379
rnd me
427 Recognized Expert Contributor
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
sangam56
68 New Member
Hi,
Thanks for the reply. I tried to use this function in my js file this way:

div.addEventLis tener=addEvent( div,"click","ap pendSignature() ");

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

Here comes an error:

...Could not convert JavaScript argument...
a.addEventListe ner(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 Recognized Expert Moderator Expert
well, two things. you shouldn't redefine the addEventListene r 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 Recognized Expert Moderator Expert
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
sangam56
68 New Member
Hi,
Thanks all for the answers.
I changed appendSignature () to appendSignature only, and it worked!

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

Thanks again!
Mar 26 '09 #6
Dormilich
8,658 Recognized Expert Moderator Expert
@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
2939
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
2863
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) Adding onmousemove="return false" onmousedown="return false" allows this to happen but I want to add events with addEventListener/attachEvent instead....
2
6067
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 place a DIV inside the first which will act as a button and when clicked will run code that's also inside the class. To create the button DIV I use...
3
2003
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 offered the chance to delete or edit it. I'm trying to figure out how that is done. It seems like either you can all LI elements to an...
10
13006
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 boldLink=document.getElementById('cmtbold');
6
3709
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 listener function which will be called when the link is clicked. events.js will have all the addEventListener() functions. ajaxModal.js contents: ...
1
2909
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 consider this code: if(choice == a) button.addEventListener("click", function () {alert('just trying
1
3661
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 event. Is there a way to list event listeners that have been wired-up to an element's event?
0
6124
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 something like var win = window.open(); win.addEventListener... it doesn't seem to work, see my code below exportwin =...
0
7908
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8336
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.