473,326 Members | 2,173 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,326 software developers and data experts.

Dynamically created html button not firing onclick()

I have a table that has rows appended dynamically, based on the item selected from a dropdownlist. The rows have a number of cells, one with a literal control using the text of the dropdownlist, several checkboxes, and a button.


I am trying to use the button to delete the newly-added row if it was added by mistake. The row with its checkboxes, label and button gets added to the table, but the onclick event doesn't fire. Here's the relevant code snippet:

Expand|Select|Wrap|Line Numbers
  1. var tbl = document.getElementById('tblSelectedServices');
  2. var tbody = tbl.tBodies[0];
  3. var newRowNum = tbl.rows.length;
  4. var svc = ddl[ddl.selectedIndex].value;
  5.  
  6. var newRow = document.createElement('TR');
  7. var delcol = document.createElement('TD');
  8.  
  9. var delbtn = document.createElement('INPUT');
  10. delbtn.setAttribute('type','button');
  11. delbtn.setAttribute('id','del' + newRowNum);
  12. delbtn.setAttribute('style','WIDTH: 17px; HEIGHT: 17px;');
  13. delbtn.setAttribute("onclick","removeRow(" + newRowNum + ");");
  14.  
  15. delcol.appendChild(delbtn)
  16. newRow.appendChild(delcol);
  17.  
  18.         ..... add more stuff .....
  19.  
  20. tbl.firstChild.appendChild(newRow);
  21.  
I can see the new row and its attributes using a DOM viewer addin, but the style doesn't appear, nor does the onclick fire.

What am I missing??
May 1 '06 #1
2 5032
I am having the same problem. I am using AJAX and am writing out links with onclick's set to call a javascript function. However, once written out to the browser the link do not respond on click.

This is the php line that creates the onclick event for the link.

....
$data = $data."<br><a href='#' onclick='post('showFile',$file');'>$file</a>";
....

Even if I replace the post function with a simple javascript alert, it doesn't call it.
I'm not sure if it would affect this, but I have a function being called in the page onLoad event. However, the anchor tag is not linking anywhere, so it shouldn't post the page.

Thanks.
Feb 1 '08 #2
iam_clint
1,208 Expert 1GB
I would do something like this.
Expand|Select|Wrap|Line Numbers
  1. //delbtn.setAttribute("onclick","removeRow(" + newRowNum + ");");
  2. delbtn.setAttribute("rownum", newRowNum);
  3. delbtn.onclick = function () { removeRow(this.getAttribute("rownum")); }
  4.  
Feb 1 '08 #3

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

Similar topics

1
by: Chi | last post by:
Hi There, The following is a popup form that works exactly as I want but I now need to get the values of each text box added when a user clicks the 'Add' button and pass the values back the...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
5
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button....
7
by: rsaffy | last post by:
I am having trouble with my dynamically created button's event handling. I read that the buttons need to be recreated on every trip to the server, but how exactly do you do that when the datagrid...
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
2
by: jmarendo | last post by:
Hello, After reading through the "Table Basics - DOM - Refer to table cells" example at mredkj.com , I modified the code for my own purposes. In the modified version, I create a hyperlink and...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
7
by: moksha | last post by:
Hi, I am new to javascript and i am facing a problem in coding. plz help me out. I am using javascript for dynamically creating a table row which contains text boxes and radio...
3
by: balurajeev | last post by:
Hii, currently i am working on an apliction(ASP.NET/C#) that creates dynamic html rows which contain six html text boxes and a html dropdowllist. At the time of page load the web page contain six...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.