![]() |
Adding/removing table rows
Hi guys. A posted a similar question before, but I have come across a different issue now. Basically, I have a table with some rows
Code:
<table class="table1">Code:
var stdContent = "<td class='title'>xx:</td>\n" +Code:
var rws;Secondly, if I am using the same class for the datepicker, why wont they work with dynamically added rows? Any information appreciated. Cheers |
Because a <tr> is *NOT* a child of the <table>, unless you (mistakenly) made it so dynamically.
When generated via HTML, a <tr> is a child of the <tbody>. And so a grandchild of the <table>. Example (that works): Code:
|
This code is actually *NOT* the preferred way to add a row to a table:
Code:
function addTableRow() {Code:
function addTableRow() {Code:
<html> |
Regarding the datepicker: The code for the datepicker looks for all elements with that given class WHEN THE PAGE IS LOADED. It attaches AT LEAST an onclick event handler to each such element.
When you add a row dynamically, you don't notify the date picker that there is a new element with that class, so it doesn't have a chance to attach the needed onclick handler. Cloning the HTML of the elements does *NOT* clone the event handlers for those elements. |
Ah, I see. Is tbody relatively new, because I never remember using it when I used tables a while back. So now it lets me remove all the rows, including the default ones, which is good. One more quick question though which is slightly JQuery, but it happens on some normal fields aswell. One of the fields in a table row is
Code:
<input type="text" name="popupdepartdate2[]" id="popupdepartdate2" class="popupdatepicker" value="Depart" />Nick |
Quote:
Code:
but then how could I get around this? |
Quote:
Modern browsers all automatically give you a <tbody>, even if you don't explicitly code one. But you can, indeed, explicitly code it. Just as you can also explicity code <thead> and <tfoot>. http://www.w3schools.com/tags/tag_tbody.asp |
Kool, thanks for your reply, I will look into that now. I just wanted to quickly ask you a question about your add and remove functions though. I tested them because they looked neater than what I had, but although it adds and removes the rows, it doesnt add the td's with the appropiate fields. In a sense, what I am doing is cloning the previous rows (changing the id's of the new fields) which is why it may actually be better to use JQuery for this. But with the code you demonstrated, was that actually suppossed to add the required fields, or just create a new row with nothing in it?
Nick |
My code was strictly a demo. Had nothing (directly) to do with yours.
Yes, I saw that you are using innerHTML to clone the <td>s. Personally, for only 3 <td>s, I wouldn't do so. I'd use insertCell and DOM properties, instead. But you can use innerHTML, if you want. |
Thank you very much for all your help, I really appreciate it. I have learned a lot of new things and now I think its time for me to tidy everything up!!
|
See, instead of using innerHTML and then readjusting everything, you could do this:
Code:
var tbl = document.getElementById("theTable"); |
| All times are GMT +1. The time now is 06:36 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.