Dynamically add, modify, and delete html tables.

Source: Internet
Author: User

Dynamically add, modify, and delete html tables.
Bytes

<Html> 
Dynamic_table.js
Onerror = handleErrvar txt = "" function handleErr (msg, url, l) {txt = "An error exists on this page. \ N "txt + =" error: "+ msg +" \ n "txt + =" URL: "+ url +" \ n "txt + =" line: "+ l +" \ n "txt + =" click "OK" to continue. \ N "alert (txt) return true} function addRow (name, color, size, unit, discount, count, sum) {var bodyObj = document. getElementById ("goods"); if (bodyObj = null) {alert ("Body of Table not Exist! "); Return;} var rowCount = bodyObj. rows. length; // var cellCount = myarray. length; var newRow = bodyObj. insertRow (rowCount ++); newRow. insertCell (0 ). innerHTML = name; newRow. insertCell (1 ). innerHTML = color; newRow. insertCell (2 ). innerHTML = size; newRow. insertCell (3 ). innerHTML = unit; newRow. insertCell (4 ). innerHTML = discount; newRow. insertCell (5 ). innerHTML = count; newRow. insertCell (6 ). innerHTML = sum;} function RemoveRow (tbodyID, row) {var bodyObj = document. getElementById (tbodyID); if (bodyObj = null) {alert ("Body of Table not Exist! "); Return;} var nrow = Number (row); if (nrow <= bodyObj. rows. length) bodyObj. deleteRow (nrow); else alert ("nrow is less. ");} function modifyRow (tbodyID, row, col, newvalue) {var nrow = Number (row); var ncol = Number (col); var bodyObj = document. getElementById (tbodyID); if (bodyObj = null) {alert ("Body of Table not Exist! "); Return;} try {// var tableObj = bodyObj. parentNode; if (nrow <bodyObj. rows. length & ncol <bodyObj. getElementsByTagName ('tr') [nrow]. getElementsByTagName ('td '). length) {// This may not work on google in ie. // bodyObj. rows (nrow ). cells (ncol ). innerHTML = newvalue; // bodyObj. rows [nrow]. childNodes [ncol]. innerHTML = newvalue; // This document can be used in both ie and google. getElementById (tbodyID ). getElementsByTagName ('tr') [nrow]. get ElementsByTagName ('td ') [ncol]. innerHTML = newvalue;} else alert ("empty. ");} catch (err) {alert (err. description) ;}} function clearRows (tbodyID) {var bodyObj = document. getElementById (tbodyID); if (bodyObj = null) {alert ("Body of Table not Exist! "); Return ;}for (var I = 0; I <bodyObj. rows. length;) bodyObj. deleteRow (I );}
Run normally under win7 ie10 and google 4. note 1. it is recommended that you do not use the notepad that comes with windows. There will be encoding problems. We recommend using notepad ++, encoding utf8 without bom2. some js functions will be available in ie and unavailable in google, this code is generic

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.