How do I create HTML table using jQuery dynamically?

后端 未结 5 2070
梦如初夏
梦如初夏 2020-12-08 20:10

I am trying to create a HTML table like the following dynamically using jQuery:



        
      
      
      
5条回答
  •  情话喂你
    2020-12-08 20:27

    You may use two options:

    1. createElement
    2. InnerHTML

    Create Element is the fastest way (check here.):

    $(document.createElement('table'));
    

    InnerHTML is another popular approach:

    $("#foo").append("
    hello world
    "); // Check similar for table too.

    Check a real example on How to create a new table with rows using jQuery and wrap it inside div.

    There may be other approaches as well. Please use this as a starting point and not as a copy-paste solution.

    Edit:

    Check Dynamic creation of table with DOM

    Edit 2:

    IMHO, you are mixing object and inner HTML. Let's try with a pure inner html approach:

    function createProviderFormFields(id, labelText, tooltip, regex) {
        var tr = '
' ; // create a new textInputBox var textInputBox = ''; // create a new Label Text tr += ''; tr += ''; tr +=''; return tr; }

提交回复
热议问题
' + labelText + '' + textInputBox + '