Introduction
jQuery has some predefined functions. Using predefined functions, we can perform our client-side tasks. jQuery append function is used to bind a table on client-side.
Why we will bind on client side
- Any operation we will perform in the client-side is more likely to be faster than the server-side because the operation is happening in the client browser.
- We can give an attractive look to the UI during client-side operation.
- Reduce the server side coding.
Example
Here we have an array named bykeList, which contains three brand names.
var bykeList=["Hero","Honda","Suzuki"]
Now we will bind this array to a Table using jQuery append function.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>Tooltip Example</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
- </head>
- <body>
-
- <div class="container">
- <table class="table table-striped">
- <thead>
- <tr>
- <th>Sl.No</th>
- <th>Byke Name</th>
- <tr>
- </thead>
- <tbody id="tblBykeLists"><tbody>
- </table>
-
- </div>
-
- <script>
- $(document).ready(function(){
- var bykeList=["Hero","Honda","Suzuki"];
- if(bykeList.length>0){
- for(i=0;i<bykeList.length;i++){
- var html="<tr><td>"+Number(1+i)+"</td><td>"+bykeList[i]+"</td></tr>";
- $("#tblBykeLists").append(html);
- }
- }
- });
- </script>
-
- </body>
- </html>
In the above code, first, I checked the bykeList length. If the length is greater than 0 then there is some data, iterate through the data and bind it in a table.
Summary
In this blog, I discussed how we can use jQuery append function. I hope it was useful for you.