wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Add Rows dynamically to a table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Add Rows dynamically to a table" Watch "Add Rows dynamically to a table" New topic
Author

Add Rows dynamically to a table

Nee Kat
Ranch Hand

Joined: Jan 27, 2004
Posts: 37
I have to add rows to a table dynamically .i.e. by using JavaScript. I was able to add row using the follwoing code I found over the net. The problem is I have to create a dropdown in couple of cells in the new added row and add certain predefined values in it using cfquery.
But with this code I was not able add that as this adds a text box. Any changes in this code to add the drop down would be helpful.

function addRowToTable()
{
var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);

// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration);
cellLeft.appendChild(textNode);

// right cell
var cellRight = row.insertCell(1);
var el = document.createElement('input');
el.setAttribute('type', 'text');
el.setAttribute('name', 'txtRow' + iteration);
el.setAttribute('id', 'txtRow' + iteration);
el.setAttribute('size', '40');
el.onekeypress = keyPressTest;
cellRight.appendChild(el);
}

I am unable to post the HTML as it is not allowed to post certain functions in JS ( I got this in the error)



Thanks!
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
Originally posted by Nee Kat:
I am unable to post the HTML as it is not allowed to post certain functions in JS ( I got this in the error)

One isn't allowed to post event-handlers (onclick, onchange etc).
Just replace the "o" by "#&111;" (#&111;nclick, #&111;nchange etc).
cb
[ October 25, 2005: Message edited by: Chris Baron ]
Nee Kat
Ranch Hand

Joined: Jan 27, 2004
Posts: 37
I have been able to create a dropdown box but still looking for a way to populate it. Please help me.

Regards,
nee
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
If the values are from the server you would have to use Ajaxor submit the page to the server. If you are filling the values from JavaScript, you can use JavaScript's new Option(). Search Google.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Add Rows dynamically to a table