JavaScript code for reading table content _javascript Tips

Source: Internet
Author: User
<ptml> <pead> <meta http-equiv= "content-language" content= "ZH-CN" > <meta http-equiv= "content-ty PE "content=" text/html; charset=gb2312 "> <title>222222 </title> </pead> <body> <table border=" 1 "width=" 45% "he ight= "id=" "table1" > <tr> <td> <input type= "checkbox" Name= "C1" value= "on" > </td> &LT;TD&G t; Cloud Community </td> <td>www.jb51.net</td> </tr> <tr> <td> <input type= "checkbox" Name= "C2" value= "on" > </td> <td> Baidu </td> <td>www.baidu.com</td> </tr> <tr> ;td> <input type= "checkbox" Name= "C3" value= "on" > </td> <td> Server Software </td> <td> s.jb51.net</td> </tr> </table> <input type= "button" onclick= "Readarray ()" value= "Get Tabular Value" > < Script>//Use array to get table value function Getrowtext () {var arr = new Array (); var count = 0; for (var i=0;i <table1.rows.length;i++) {var row = Table1.rows[i]; Get checkbox var Objcheck = row.cells[0].getelementsbytagname ("INPUT") [0]; If the checkbox is selected. Add to two-dimensional array if (Objcheck!=null && objcheck.checked) {//arr[i]= row.innertext; Arr[count] = new Array (); for (var x=0;x <row.cells.length;x++) {//Excluding the cell contents of the Radio box if (x==0) continue; Arr[count][x-1] = Row.cells[x].innertext; } count++; } return arr; //Read table value function Readarray () {var arr = Getrowtext (); for (var i=0;i <arr.length;i++) {//Get the value of each row for (Var x= 0;x <arr[i].length;x++) {document.write (arr[i][x)); }} </script> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Definitions and usage
The Rows collection returns an array of all rows (TableRow objects) in the table, that is, a htmlcollection.

The collection includes all the lines defined in <thead>, <tfoot>, and <tbody>.

Grammar
Tableobject.rows[] Instance
The following example uses the Rows collection to display the contents of the first row:
<ptml> <pead> <script type= "Text/javascript" > F Unction Showrow () {Alert (document.getElementById (' myTable '). rows[0].innerhtml)} </script> </pead> < body> <table id= "myTable" border= "1" > <tr> <td>row1 cell1</td> <td>row1-cell2</td > </tr> <tr> <td>row2 cell1</td> <td>row2 cell2</td> </tr> </TABLE&G T <input type= "button" onclick= "Showrow ()" value= "show InnerHTML" > </body> </ptml>
[ctrl+a All selected note: If you need to introduce external JS need to refresh to perform ]
Related Article

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.