Dynamically create span, input, select, and table columns

Source: Internet
Author: User
  1. /*
  2. * Parameters:
  3. * Textvalue: Text value (to create a text cell text value)
  4. * Idname: ID value (to create a text Cell ID)
  5. */
  6. Function createtexte (OBJ, idname, textvalue ){
  7. VaR span = Document. createelement ("span ");
  8. Span. setattribute ("ID", idname );
  9. Span. appendchild (text1 = Document. createtextnode (textvalue ));
  10. OBJ. appendchild (SPAN );
  11. }
  12. /*
  13. * Adds an input element (new) to a specific cell)
  14. * Parameters:
  15. * OBJ: The Host Cell Object of the input element to be created.
  16. * Textvalue: Text value (to create a text cell text value)
  17. * Idname: ID value (to create a text Cell ID)
  18. */
  19. Function createinpute (OBJ, idname, inputtype, inputvalue ){
  20. VaR span = Document. createelement ("span ");
  21. Span. setattribute ("ID", idname );
  22. VaR input = Document. createelement ("input ");
  23. Input. setattribute ("type", inputtype );
  24. Input. setattribute ("value", inputvalue );
  25. Span. appendchild (input );
  26. OBJ. appendchild (SPAN );
  27. }
  28. /*
  29. * Used to generate select elements
  30. */
  31. Function createselecte (OBJ, idname ){
  32. VaR span = Document. createelement ("span ");
  33. Span. setattribute ("ID", idname );
  34. VaR select = Document. createelement ("select"); // create a select Element
  35. VaR op = Document. createelement ("option"); // create an option (OP)
  36. Op. setattribute ("value", 0); // you can specify the value of option.
  37. Op. appendchild (document. createtextnode ("Select ---"); // set the text of Option
  38. Select. appendchild (OP); // creates an option (OP) for select)
  39. Span. appendchild (select );
  40. OBJ. appendchild (SPAN );
  41. }
  42. --------------------------------------------------------------------
  43. Example
  44. VaR Index = 0;
  45. Function addrow (){
  46. Index ++;
  47. // The table (tbody) to add or delete rows)
  48. T = Document. getelementbyid ("omain ");
  49. Objrow = T. insertrow (0); // Add a row to the table, that is, <tr>
  50. Objcell = objrow. insertcell (0); // Add a cell in the row, that is, <TD>
  51. Objcell. setattribute ("ID", "goodsid ");
  52. Createtexte (objcell, "goodsid", index); // item No. goodsid
  53. Objcell = objrow. insertcell (1); // Add a cell in the row, that is, <TD>
  54. Objcell. setattribute ("ID", "goodsname ");
  55. Createselecte (objcell, "goodsname ");
  56. Objcell = objrow. insertcell (2); // Add a cell in the row, that is, <TD>
  57. Objcell. setattribute ("ID", "price ");
  58. Createtexte (objcell, "price", "2.3 (unit price )");
  59. Objcell = objrow. insertcell (3); // Add a cell in the row, that is, <TD>
  60. Objcell. setattribute ("ID", "Count ");
  61. Createinpute (objcell, "Count", "text", index); // Number of days to order
  62. Objcell = objrow. insertcell (4); // Add a cell in the row, that is, <TD>
  63. Objcell. setattribute ("ID", "dcount ");
  64. Createinpute (objcell, "dcount", "text", index); // daily quantity
  65. Objcell = objrow. insertcell (5); // Add a cell in the row, that is, <TD>
  66. Objcell. setattribute ("ID", "totleprice ");
  67. Createtexte (objcell, "totleprice", "Total" + index); // total price
  68. Objcell = objrow. insertcell (6); // Add a cell in the row, that is, <TD>
  69. Objcell. setattribute ("ID", "send ");
  70. Createtexte (objcell, "send", "get" + index );//
  71. Objcell = objrow. insertcell (7); // Add a cell in the row, that is, <TD>
  72. Objcell. setattribute ("ID", "bdel ");
  73. Createinpute (objcell, "bdel", "button", "delete" + index );
  74. }

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.