Connecting Tech Pros Worldwide Forums | Help | Site Map

adding an event handler

Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#1: Sep 1 '08
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.

Expand|Select|Wrap|Line Numbers
  1. // view icon
  2.               var cellview = row.insertCell(3);
  3.               var view = document.createElement("img");
  4.               view.src="view.gif";
  5.               view.onClick=function(){go();}
  6.               view.alt = "View";
  7.               cellview.appendChild(view);
Heres the full code for the table just to make things a little clearer
Expand|Select|Wrap|Line Numbers
  1. function updateProjects() {
  2.     if (request.readyState == 4) {
  3.         var returned = request.responseText;
  4.         // alert(returned);
  5.         var splitResult = returned.split("^");
  6.             for (var i=0;i<splitResult.length-1;i++) {
  7.             var record = splitResult[i];
  8.             // alert(record);
  9.             var splitRecord = record.split("$");
  10.             var tbl = document.getElementById('incomplete');
  11.             var lastRow = tbl.rows.length;
  12.             var iteration = lastRow;
  13.               var row = tbl.insertRow(lastRow);
  14.  
  15.               // cell number
  16.               var cellLeft = row.insertCell(0);
  17.               var textNode = document.createTextNode(iteration);
  18.               cellLeft.appendChild(textNode);
  19.  
  20.               // company
  21.               var cellcomp = row.insertCell(1);
  22.               var t_comp = document.createTextNode(splitRecord[0]);
  23.               cellcomp.appendChild(t_comp);
  24.  
  25.               // project
  26.               var cellproj = row.insertCell(2);
  27.               var t_proj = document.createTextNode(splitRecord[1]);
  28.               cellproj.appendChild(t_proj);
  29.  
  30.               // view icon
  31.               var cellview = row.insertCell(3);
  32.               var view = document.createElement("img");
  33.               view.src="view.gif";
  34.               view.onClick=function(){go();}
  35.               view.alt = "View";
  36.               cellview.appendChild(view);
  37.  
  38.         }
  39.     }
  40. }

RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 349
#2: Sep 1 '08

re: adding an event handler


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
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#3: Sep 1 '08

re: adding an event handler


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

Expand|Select|Wrap|Line Numbers
  1. // view icon
  2.               var cellview = row.insertCell(3);
  3.               var newObject = document.createElement('div');
  4.             var proj = splitRecord[2];
  5.             // alert (proj);
  6.             var view = document.createElement("img");
  7.               view.src="view.gif";
  8.             newObject.appendChild(view);
  9.             newObject.onmousedown=function(){document.location.href = "/PanelView.php?proj=" + escape(proj);}
  10.               view.alt = "View";
  11.               cellview.appendChild(newObject);
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Sep 1 '08

re: adding an event handler


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?
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 349
#5: Sep 1 '08

re: adding an event handler


I am glad that u got the solution.


Regards
Ramanan Kalirajan
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#6: Sep 1 '08

re: adding an event handler


proj is the variable that needs to be different. I keep getting the value of proj for the last record.
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 349
#7: Sep 1 '08

re: adding an event handler


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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Sep 1 '08

re: adding an event handler


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.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#9: Sep 1 '08

re: adding an event handler


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.
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#10: Sep 1 '08

re: adding an event handler


Im even more confused now than I was before. Thanks lol.
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#11: Sep 1 '08

re: adding an event handler


confused by what specifically? :)

kind regards
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#12: Sep 1 '08

re: adding an event handler


Trying to implement the closures in the loop as suggested in acoders last post
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 349
#13: Sep 1 '08

re: adding an event handler


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
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#14: Sep 1 '08

re: adding an event handler


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:

Expand|Select|Wrap|Line Numbers
  1. var ele = document.getElementsByTagName('*');
  2.  
  3. for (var i = 0; i < ele.length; i++) {
  4.     ele[i].onclick = function(val) {
  5.         return function() { alert(ele[val].tagName) };
  6.     }(i);
  7. }
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
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#15: Sep 2 '08

re: adding an event handler


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;

Expand|Select|Wrap|Line Numbers
  1. function updateCompleteProjects() {
  2.     //alert(request.readyState);
  3.     if (request.readyState == 4) {
  4.         //alert("");
  5.         var returned = request.responseText;
  6.         //alert("");
  7.         var splitResult = returned.split("^");
  8.             for (var i=0;i<splitResult.length-1;i++) {
  9.             var record = splitResult[i];
  10.             // alert(record);
  11.             var splitRecord = record.split("$");
  12.             var tbl = document.getElementById('panels');
  13.             var lastRow = tbl.rows.length;
  14.             var iteration = lastRow;
  15.               var row = tbl.insertRow(lastRow);
  16.  
  17.               // cell number
  18.               var cellLeft = row.insertCell(0);
  19.               var textNode = document.createTextNode(iteration);
  20.               cellLeft.appendChild(textNode);
  21.  
  22.               // company
  23.               var cellcomp = row.insertCell(1);
  24.               var t_comp = document.createTextNode(splitRecord[0]);
  25.               cellcomp.appendChild(t_comp);
  26.  
  27.               // project
  28.               var cellproj = row.insertCell(2);
  29.               var t_proj = document.createTextNode(splitRecord[1]);
  30.               cellproj.appendChild(t_proj);
  31.  
  32.               // project id
  33.               var cellprojid = row.insertCell(3);
  34.               var t_projid = document.createTextNode(splitRecord[2]);
  35.               cellprojid.appendChild(t_projid);
  36.  
  37.               // price icon
  38.               var cellprice = row.insertCell(4);
  39.               var newObject1 = document.createElement('div');
  40.             var price = document.createElement("img");
  41.               price.src="price.png";
  42.             newObject1.appendChild(price);
  43.             price.alt = "Price";
  44.               cellprice.appendChild(newObject1);
  45.  
  46.               // view icon
  47.               var cellview = row.insertCell(5);
  48.               var newObject = document.createElement('div');
  49.               newObject.Class ="view";
  50.               var ele = document.getElementsByTagName('div');
  51.               var view = document.createElement("img");
  52.               view.src="view.gif";
  53.               view.alt = "View";
  54.             newObject.appendChild(view);
  55.             for (var i = 0; i < ele.length; i++) {
  56.             ele[i].onclick = function(proj) {
  57.                 var proj = splitRecord[2];
  58.                 //alert(proj);
  59.                    return function() { document.location.href = "/PanelView.php?proj=" + escape(proj); };
  60.                 }(i);
  61.             }
  62.               cellview.appendChild(newObject);
  63.  
  64.         }
  65.         getIncompleteProjects();
  66.     }
  67.  
  68. }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#16: Sep 2 '08

re: adding an event handler


With the code on line 57:
Expand|Select|Wrap|Line Numbers
  1. var proj = splitRecord[2];
you're using a local variable, not the passed i value as the parameter proj.
Familiar Sight
 
Join Date: Jun 2008
Posts: 164
#17: Sep 2 '08

re: adding an event handler


how do I use the i value of it?
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,134
#18: Sep 2 '08

re: adding an event handler


have a close look at what you are doing:

Expand|Select|Wrap|Line Numbers
  1. ele[i].onclick = function(proj) {
  2.     var proj = splitRecord[2];
  3.  
  4.     // ...
  5. }(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
Reply