JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 26th, 2005, 01:53 PM
fgaudet fgaudet is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 2 fgaudet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 53 sec
Reputation Power: 0
Angry How to dynamically bind events on dynamically created content?

Hello all, here's what I'm trying to do:

- create a data entry table dynamically w. javascript
- bind events to this grid

I get the first part done, but my events never bind!!

sample event binding code:
Code:
for(var i=0; i<tbody.rows.length; i++)
  {tbody.rows[i].cells[j].onclick = MyCustom_onClick;}
(tried all variants possible: onClick, click, Click, etc)

also tried:
Code:
tbody.rows[i].cells[j].addEventListener("click", MyCustom_onClick, false);


haven't tried, and would rather have a solution more like above:
Code:
tbody[....].setAttribute('onclick', 'MyCustom_onClick()')
  

Anybody have an idea on how to do this? Can it be done?

tia!

Reply With Quote
  #2  
Old January 27th, 2005, 08:35 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Your first sample looks good.
The problem might be with the declaration of MyCustom_onClick

Code:
function MyCustom_onClick(e) {
...
}


e will refer to the event being called

Here's an example I made up:
Code:
<html>
<head>
<title>Sample code - Traversing an HTML Table with JavaScript and DOM Interfaces</title>
<script type="text/javascript">
<!--
	function start() {

		var mybody=document.getElementsByTagName("body").item(0);

		myInput = document.createElement("input");
		myInput.type='button';
		myInput.value='Click me';
		myInput.style.curosr='pointer'
		myInput.onclick=doSomething;
		mybody.appendChild(myInput);
	}
	 
	 function doSomething(e) {
	 	alert('you clicked!');
	 }
//-->
</script>
</head>
<body onload="javascript:start()">
<h1>Dynamically adding events</h1>

<p>This script will dynamically draw a button, and append an event to the button.</p>
</body>
</html>

Reply With Quote
  #3  
Old January 27th, 2005, 10:05 AM
fgaudet fgaudet is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 2 fgaudet User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 53 sec
Reputation Power: 0
Thanks MadCowDzz,

you were right, something was wrong in the declaration of MyCustom_onClick!

Yet somehow, it was working in IE, but not in Firefox in which I was using :

Code:
   cell.setAttribute('onclick', 'MyCustom_onClick('+ cell.id +')');


After revising my code, the
Code:
cell.onclick = MyCustom_onClick;
now works in both browsers!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJavaScript Development > How to dynamically bind events on dynamically created content?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT