Get text and id from an li element on click with pure JS

前端 未结 2 1968
逝去的感伤
逝去的感伤 2021-01-28 22:12

I\'ve been stuck with this for several days and I can\'t solve it.

I\'ve done it with jQuery with no problem, but I need it in pure JS.

This is how my list is ge

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-28 23:06

    Jsfiddle

    Combination of james' answer and working example.

    function get_friends(items) {
        if (items != undefined) {
            if (items.length != 0) {
                var html_friends_list = "
      "; for (var count = 0; count < items.length; count++) { if (items[count].subscription == "both") { html_friends_list = html_friends_list + "
    • "+ items[count].display_name +"
    • "; } } html_friends_list = html_friends_list + '
    ' document.getElementById("friends-list").innerHTML = html_friends_list; } } }

    Note: you should trigger prototype after your dom element created.

提交回复
热议问题