最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How do I append a button to a list item with JavaScript? - Stack Overflow

programmeradmin5浏览0评论

This function is run every time an item is added to an array. It simply creates a list item element, appends some text to that item, and then appends it to a list element (#requestList).

function UpdateListOnScreen(NewListItem){
  
  var grabList = document.getElementById('requestList');
  
  var text = ""+ GetCalendarName(NewListItem.calChoice) +" For "+ GetLessonSlot(NewListItem.lessonChoice) + " On " + GetDateInTextForm(NewListItem.date) + "";
  var entry = document.createElement('li');
  entry.id = list.length-1;
  entry.className = "ItemNotChecked";
  entry.appendChild(document.createTextNode(text));
  grabList.appendChild(entry);
}

Something I'm struggling to figure out is how do I add a button element onto that list item? I want every list item to have its own button element, but can't seem to figure out how I append a button to the list item after appending the text to the list item.

Thanks.

This function is run every time an item is added to an array. It simply creates a list item element, appends some text to that item, and then appends it to a list element (#requestList).

function UpdateListOnScreen(NewListItem){
  
  var grabList = document.getElementById('requestList');
  
  var text = ""+ GetCalendarName(NewListItem.calChoice) +" For "+ GetLessonSlot(NewListItem.lessonChoice) + " On " + GetDateInTextForm(NewListItem.date) + "";
  var entry = document.createElement('li');
  entry.id = list.length-1;
  entry.className = "ItemNotChecked";
  entry.appendChild(document.createTextNode(text));
  grabList.appendChild(entry);
}

Something I'm struggling to figure out is how do I add a button element onto that list item? I want every list item to have its own button element, but can't seem to figure out how I append a button to the list item after appending the text to the list item.

Thanks.

Share Improve this question edited Jul 4, 2023 at 10:13 Tushar Gupta 15.9k1 gold badge32 silver badges52 bronze badges asked Mar 3, 2015 at 11:44 Darren ReederDarren Reeder 631 gold badge1 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

This is an example how you can insert button :

var ul = document.getElementById("list");
var li = document.createElement("li");
li.appendChild(document.createTextNode("Four"));
var button = document.createElement("button");
button.innerHTML = "asdasd";
li.appendChild(button);
li.setAttribute("id","element4");
ul.appendChild(li);
alert(li.id);
<ul id="list"></ul>

This should do the trick:

function UpdateListOnScreen(NewListItem) { 
  var grabList = document.getElementById('requestList');

  var text = "" + GetCalendarName(NewListItem.calChoice) + " For " + GetLessonSlot(NewListItem.lessonChoice) + " On " + GetDateInTextForm(NewListItem.date) + "";
  var entry = document.createElement('li');
  entry.id = list.length - 1;
  entry.className = "ItemNotChecked"; 
  entry.appendChild(document.createTextNode(text));

  /*Add a button to each LI */
  var button = document.createElement('button');
  button.innerText = 'Click me!';
  entry.appendChild(button);

  grabList.appendChild(entry);
}
发布评论

评论列表(0)

  1. 暂无评论