Connecting Tech Pros Worldwide Help | Site Map

adding an event handler

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 1st, 2008, 07:59 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 161
Default 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.

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. }
Reply
  #2  
Old September 1st, 2008, 08:24 AM
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Age: 24
Posts: 297
Default

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
Reply
  #3  
Old September 1st, 2008, 08:27 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 161
Default

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);
Reply
  #4  
Old September 1st, 2008, 08:44 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,228
Default

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?
Reply
  #5  
Old September 1st, 2008, 08:44 AM
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Age: 24
Posts: 297
Default

I am glad that u got the solution.


Regards
Ramanan Kalirajan
Reply
  #6  
Old September 1st, 2008, 08:50 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 161
Default

proj is the variable that needs to be different. I keep getting the value of proj for the last record.
Reply
  #7  
Old September 1st, 2008, 08:50 AM
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Age: 24
Posts: 297
Default

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
Reply
  #8  
Old September 1st, 2008, 09:32 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,228
Default

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.
Reply
  #9  
Old September 1st, 2008, 10:14 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,228
Default

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.
Reply
  #10  
Old September 1st, 2008, 11:37 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 161
Default

Im even more confused now than I was before. Thanks lol.
Reply
  #11  
Old September 1st, 2008, 12:18 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 37
Posts: 3,780
Default

confused by what specifically? :)

kind regards
Reply
  #12  
Old September 1st, 2008, 12:37 PM
Familiar Sight
 
Join Date: Jun 2008
Posts: 161
Default

Trying to implement the closures in the loop as suggested in acoders last post
Reply
  #13  
Old September 1st, 2008, 12:48 PM
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Age: 24
Posts: 297
Default

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
Reply
  #14  
Old September 1st, 2008, 12:56 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 37
Posts: 3,780
Default

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
Reply
  #15  
Old September 2nd, 2008, 09:58 AM
Familiar Sight
 
Join Date: Jun 2008
Posts: 161
Default

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. }
Reply
  #16  
Old September 2nd, 2008, 12:05 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,228
Default

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.
Reply
  #17  
Old September 2nd, 2008, 01:00 PM
Familiar Sight
 
Join Date: Jun 2008
Posts: 161
Default

how do I use the i value of it?
Reply
  #18  
Old September 2nd, 2008, 03:34 PM
gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Age: 37
Posts: 3,780
Default

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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

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 220,662 network members.