 | 
September 1st, 2008, 08:59 AM
| | Familiar Sight | | Join Date: Jun 2008
Posts: 142
| | adding an event handler
hi,
I am dynamically generating a table in my application depending on what records exist in the database. The problem I am having is when I try to assign an event handler to each record. I am including an image at the end o each row that when clicked should run a function. Heres my code but the onClick doesn't work. - // view icon
-
var cellview = row.insertCell(3);
-
var view = document.createElement("img");
-
view.src="view.gif";
-
view.onClick=function(){go();}
-
view.alt = "View";
-
cellview.appendChild(view);
Heres the full code for the table just to make things a little clearer - function updateProjects() {
-
if (request.readyState == 4) {
-
var returned = request.responseText;
-
// alert(returned);
-
var splitResult = returned.split("^");
-
for (var i=0;i<splitResult.length-1;i++) {
-
var record = splitResult[i];
-
// alert(record);
-
var splitRecord = record.split("$");
-
var tbl = document.getElementById('incomplete');
-
var lastRow = tbl.rows.length;
-
var iteration = lastRow;
-
var row = tbl.insertRow(lastRow);
-
-
// cell number
-
var cellLeft = row.insertCell(0);
-
var textNode = document.createTextNode(iteration);
-
cellLeft.appendChild(textNode);
-
-
// company
-
var cellcomp = row.insertCell(1);
-
var t_comp = document.createTextNode(splitRecord[0]);
-
cellcomp.appendChild(t_comp);
-
-
// project
-
var cellproj = row.insertCell(2);
-
var t_proj = document.createTextNode(splitRecord[1]);
-
cellproj.appendChild(t_proj);
-
-
// view icon
-
var cellview = row.insertCell(3);
-
var view = document.createElement("img");
-
view.src="view.gif";
-
view.onClick=function(){go();}
-
view.alt = "View";
-
cellview.appendChild(view);
-
-
}
-
}
-
}
| 
September 1st, 2008, 09:24 AM
|  | Familiar Sight | | Join Date: Mar 2008 Location: Chennai - India Age: 24
Posts: 243
| |
Just try this code, after the cellview.appendChild(view); just insert a line
[HTML] view icon
var cellview = row.insertCell(3);
var view = document.createElement("img");
view.src="view.gif";
view.onClick=function(){go();}
view.alt = "View";
cellview.appendChild(view);
cellview.innerHTML = cellview.innerHTML
[/HTML]
hope it will work. If any prob post back it to the forum
Regards
Ramanan Kalirajan
| 
September 1st, 2008, 09:27 AM
| | Familiar Sight | | Join Date: Jun 2008
Posts: 142
| |
I have managed to add the event handler by appending the image to a div and adding the event handler to the div. The problem I now face is that the variable I assign needs to be different for each record. Heres my code - // view icon
-
var cellview = row.insertCell(3);
-
var newObject = document.createElement('div');
-
var proj = splitRecord[2];
-
// alert (proj);
-
var view = document.createElement("img");
-
view.src="view.gif";
-
newObject.appendChild(view);
-
newObject.onmousedown=function(){document.location.href = "/PanelView.php?proj=" + escape(proj);}
-
view.alt = "View";
-
cellview.appendChild(newObject);
| 
September 1st, 2008, 09:44 AM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
The problem with the code in the first post was that "onClick" needed to be "onclick". JavaScript is case-sensitive. Which variable needs to be different?
| 
September 1st, 2008, 09:44 AM
|  | Familiar Sight | | Join Date: Mar 2008 Location: Chennai - India Age: 24
Posts: 243
| |
I am glad that u got the solution.
Regards
Ramanan Kalirajan
| 
September 1st, 2008, 09:50 AM
| | Familiar Sight | | Join Date: Jun 2008
Posts: 142
| |
proj is the variable that needs to be different. I keep getting the value of proj for the last record.
| 
September 1st, 2008, 09:50 AM
|  | Familiar Sight | | Join Date: Mar 2008 Location: Chennai - India Age: 24
Posts: 243
| | Quote: |
Originally Posted by acoder The problem with the code in the first post was that "onClick" needed to be "onclick". JavaScript is case-sensitive. Which variable needs to be different? | I am having a question to u Mr. Acoder while I am developing some dynamic object in HTML with Javascripts I used to get the same kind of problem, there will be no error in the code, but my script wont work. when i asked about this prob my mentor told that your container needs to refreshed. So i used myContianer.innerHTML = myContainer.innerHTML. I got my scripting work. Wether this is the reason for the problem.
Regards
Ramanan Kalirajan
| 
September 1st, 2008, 10:32 AM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| | Quote: |
Originally Posted by cleary1981 proj is the variable that needs to be different. I keep getting the value of proj for the last record. | This is a common mistake when using closures in loops. To solve it, create another closure as described here.
| 
September 1st, 2008, 11:14 AM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| | Quote: |
Originally Posted by RamananKalirajan I am having a question to u Mr. Acoder while I am developing some dynamic object in HTML with Javascripts I used to get the same kind of problem, there will be no error in the code, but my script wont work. when i asked about this prob my mentor told that your container needs to refreshed. So i used myContianer.innerHTML = myContainer.innerHTML. I got my scripting work. Wether this is the reason for the problem. | That looks like a terrible hack. I'd say look at the root of the problem and try to solve that rather than use a hack like this.
| 
September 1st, 2008, 12:37 PM
| | Familiar Sight | | Join Date: Jun 2008
Posts: 142
| |
Im even more confused now than I was before. Thanks lol.
| 
September 1st, 2008, 01:18 PM
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany Age: 36
Posts: 3,448
| |
confused by what specifically? :)
kind regards
| 
September 1st, 2008, 01:37 PM
| | Familiar Sight | | Join Date: Jun 2008
Posts: 142
| |
Trying to implement the closures in the loop as suggested in acoders last post
| 
September 1st, 2008, 01:48 PM
|  | Familiar Sight | | Join Date: Mar 2008 Location: Chennai - India Age: 24
Posts: 243
| | Quote: |
Originally Posted by acoder That looks like a terrible hack. I'd say look at the root of the problem and try to solve that rather than use a hack like this. | Thank You Mr.Acode I will try to solve the problem instead of using the hack.
Regards
Ramanan Kalirajan
| 
September 1st, 2008, 01:56 PM
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany Age: 36
Posts: 3,448
| | Quote: |
Originally Posted by cleary1981 Trying to implement the closures in the loop as suggested in acoders last post | in loops it should look like the following example: - var ele = document.getElementsByTagName('*');
-
-
for (var i = 0; i < ele.length; i++) {
-
ele[i].onclick = function(val) {
-
return function() { alert(ele[val].tagName) };
-
}(i);
-
}
this closures the value of i and so assigns the correct handler ... without that outer function around the inner one you will always get the tagName of the last ele alerted - the explaination is found in the link that acoder showed you ...
kind regards
| 
September 2nd, 2008, 10:58 AM
| | Familiar Sight | | Join Date: Jun 2008
Posts: 142
| |
I have tried implementing the function as suggested but the problems I am facing are that it assigns the onclick to the very first div and the value assigned is the value of the last record. The first div is the price icon and the function is supposed to be for the view icon Heres my code; - function updateCompleteProjects() {
-
//alert(request.readyState);
-
if (request.readyState == 4) {
-
//alert("");
-
var returned = request.responseText;
-
//alert("");
-
var splitResult = returned.split("^");
-
for (var i=0;i<splitResult.length-1;i++) {
-
var record = splitResult[i];
-
// alert(record);
-
var splitRecord = record.split("$");
-
var tbl = document.getElementById('panels');
-
var lastRow = tbl.rows.length;
-
var iteration = lastRow;
-
var row = tbl.insertRow(lastRow);
-
-
// cell number
-
var cellLeft = row.insertCell(0);
-
var textNode = document.createTextNode(iteration);
-
cellLeft.appendChild(textNode);
-
-
// company
-
var cellcomp = row.insertCell(1);
-
var t_comp = document.createTextNode(splitRecord[0]);
-
cellcomp.appendChild(t_comp);
-
-
// project
-
var cellproj = row.insertCell(2);
-
var t_proj = document.createTextNode(splitRecord[1]);
-
cellproj.appendChild(t_proj);
-
-
// project id
-
var cellprojid = row.insertCell(3);
-
var t_projid = document.createTextNode(splitRecord[2]);
-
cellprojid.appendChild(t_projid);
-
-
// price icon
-
var cellprice = row.insertCell(4);
-
var newObject1 = document.createElement('div');
-
var price = document.createElement("img");
-
price.src="price.png";
-
newObject1.appendChild(price);
-
price.alt = "Price";
-
cellprice.appendChild(newObject1);
-
-
// view icon
-
var cellview = row.insertCell(5);
-
var newObject = document.createElement('div');
-
newObject.Class ="view";
-
var ele = document.getElementsByTagName('div');
-
var view = document.createElement("img");
-
view.src="view.gif";
-
view.alt = "View";
-
newObject.appendChild(view);
-
for (var i = 0; i < ele.length; i++) {
-
ele[i].onclick = function(proj) {
-
var proj = splitRecord[2];
-
//alert(proj);
-
return function() { document.location.href = "/PanelView.php?proj=" + escape(proj); };
-
}(i);
-
}
-
cellview.appendChild(newObject);
-
-
}
-
getIncompleteProjects();
-
}
-
-
}
| 
September 2nd, 2008, 01:05 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,964
| |
With the code on line 57: - var proj = splitRecord[2];
you're using a local variable, not the passed i value as the parameter proj.
| 
September 2nd, 2008, 02:00 PM
| | Familiar Sight | | Join Date: Jun 2008
Posts: 142
| |
how do I use the i value of it?
| 
September 2nd, 2008, 04:34 PM
|  | Moderator | | Join Date: May 2007 Location: Munich, Germany Age: 36
Posts: 3,448
| |
have a close look at what you are doing: - ele[i].onclick = function(proj) {
-
var proj = splitRecord[2];
-
-
// ...
-
}(i);
you instantly call a function and pass i to it ... as parameter proj. now inside this function you declare a new variable with the same name and this just drops you the passed parameter ... and you loose it here. the strict-warning you would get is: var proj hides argument ... and that gives you the hint that you drop an argument here. what should be done with the argument in your case? something should be splitted? so the argument just passes the value of i to the inner function and there you should know what to do with it?
kind regards
|  | | Thread Tools | Search 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 HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 205,248 network members.
|