Display all items in array using jquery

后端 未结 6 1365
悲&欢浪女
悲&欢浪女 2020-12-04 19:07

I have an array that I want to have displayed in html.. I don\'t want to write multiple lines for posting each item in the array but all should be exactly the same. ie

6条回答
  •  醉话见心
    2020-12-04 19:15

    Original from Sept. 13, 2015:
    Quick and easy.

    $.each(yourArray, function(index, value){
        $('.element').html( $('.element').html() + '' + value +'')
    });
    

    Update Sept 9, 2019: No jQuery is needed to iterate the array.

    yourArray.forEach((value) => {
        $(".element").html(`${$(".element").html()}${value}`);
    });
    
    /* --- Or without jQuery at all --- */
    
    yourArray.forEach((value) => {
        document.querySelector(".element").innerHTML += `${value}`;
    });
    

提交回复
热议问题