How can I create rows of data using jquery append()?

gohn

How can I make a new row of data using jquery? For example i have a div with an id "box" and I have two spans each with an id of "name" and "time". How can I have jquery append to this box holding both name and time? I tried experimenting and tried this code, but didn't work.

$("#button").click(function(){
    $("#box").append(
        $("#name").text("username"),
        $("#time").text("5:00pm")
    ); 
)}

In this code, I expected the box to create a new row of data every time I click the button. So if I want 5 rows of data, I would just click the button 5 times.

mplungjan

IDs must be unique and you need new spans

$("#button").on("click",function() {
  $("#box").append(
    "<br><span>username</span> <span>5:00pm</span>"
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<button type="button" id="button">Click</button>
<div id="box"></div>

With vars, you can use a template literal

var cnt=0, 
    data = [{ username: "John", time: "05:00pm" },
            { username: "Paul", time: "07:00pm" }];

$("#button").on("click", function() {
  if (cnt < data.length) {
    $("#box").append(
      `<br><span class="user">${data[cnt].username}</span> <span class="time">${data[cnt++].time}</span>`
    );
  }
});
.user { color:green }
.time { color: red }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<button type="button" id="button">Click</button>
<div id="box"></div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i display data using Jquery append()

How can I append array using jQuery?

How can I get number from each id and append to another data attribute using jquery?

how can I create a new data frame using exact rows from the old data frame in R Studio?

How can I create a new series by using specific rows and columns of a pandas data frame?

How can i create with fastest method 250.000.000.000 rows data

How can I append the data to follow columns instead of rows and add more column to input in sheet input data?

How can I load data from a service that returns JSON and create content using jQuery?

How to append data to table using jquery

How to append the data only once using jquery

How can I append element to the DataFrame with this data?

How can I get append option data?

How to append an <i> tag to an <a> tag using jquery

how to create and append multiple rows using two loops in python

How I can create post like system using jquery

How can I create a movie effect with a few images using jquery?

How can I create a new array and append it to the source array using map + arrow function?

How do I create and append data from csv file to big query and partition the table using python?

How can I use jquery .when to access json data to append to a page?

How can I create an interactive list using MySql data in python

How can I create a table using factor data

How can I create multiple shades in dygraphs using a data frame?

How can I create a pointer to existing data using the LuaJIT FFI?

R - How can i create graphs using an Aggregate data table?

How can I create rows and columns for buttons?

how I can create a list of rows of double[,]

In Tableau how can I create a calculated field that categorizes data based on data across rows?

How can i append a div after 5 results gotten from the database have been appended using jquery?

How do I append a range of JSON data to my HTML using Javascript or Jquery?