473,405 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

adding an event handler

178 100+
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. }
Sep 1 '08 #1
17 1643
RamananKalirajan
608 512MB
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
Sep 1 '08 #2
cleary1981
178 100+
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);
Sep 1 '08 #3
acoder
16,027 Expert Mod 8TB
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?
Sep 1 '08 #4
RamananKalirajan
608 512MB
I am glad that u got the solution.


Regards
Ramanan Kalirajan
Sep 1 '08 #5
cleary1981
178 100+
proj is the variable that needs to be different. I keep getting the value of proj for the last record.
Sep 1 '08 #6
RamananKalirajan
608 512MB
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
Sep 1 '08 #7
acoder
16,027 Expert Mod 8TB
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.
Sep 1 '08 #8
acoder
16,027 Expert Mod 8TB
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.
Sep 1 '08 #9
cleary1981
178 100+
Im even more confused now than I was before. Thanks lol.
Sep 1 '08 #10
gits
5,390 Expert Mod 4TB
confused by what specifically? :)

kind regards
Sep 1 '08 #11
cleary1981
178 100+
Trying to implement the closures in the loop as suggested in acoders last post
Sep 1 '08 #12
RamananKalirajan
608 512MB
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
Sep 1 '08 #13
gits
5,390 Expert Mod 4TB
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
Sep 1 '08 #14
cleary1981
178 100+
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. }
Sep 2 '08 #15
acoder
16,027 Expert Mod 8TB
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.
Sep 2 '08 #16
cleary1981
178 100+
how do I use the i value of it?
Sep 2 '08 #17
gits
5,390 Expert Mod 4TB
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
Sep 2 '08 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
2
by: Tim Marsden | last post by:
Hi, This is what I am doing, please comment if this is the correct way. I need to add controls to a form dynamically. Within the Page_Load event (is not Postback) I run a routine to create the...
2
by: Shiju Poyilil | last post by:
Hi ! I have a requirement wherein i am binding a datalist which contains a label (Caption for the field) and some literal hidden fields and a dropdown list. When I am binding to the datalist.....
4
by: PK9 | last post by:
I have a button at the top of my page that I have an onClick event handler for which makes some new controls (at the bottom of my page) visible. When the user clicks the button I make the new...
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
6
by: Just Me | last post by:
I need to create a library sub that adds event handlers to a control. Public Shared Sub AddEvent(ByVal qq As Control, By?? ControlMouseMove As ??) So I need to pass the address of the handler or...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.