|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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;}
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! |
|
#2
|
||||
|
||||
|
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>
|
|
#3
|
|||
|
|||
|
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; |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > How to dynamically bind events on dynamically created content? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|