javascript - How to append <li> to a <ul> on button click -
how append <li>
<ul>
on button click.
i have tried below script, want have number appended end of <li>
. item 0, item 1, item 2, etc..
var list = document.getelementbyid("list"); var add = document.getelementbyid('addelem'); add.addeventlistener('click', function() { var itemsbytagname = document.getelementsbytagname("li"); list.innerhtml += '<li>item</li>' });
<ul id="list"> <li>item </li> </ul> <button id="addelem">add</button>
here example started.
note: production use might need handle variable i
through server/session/local storage variable
.
var = 1; var list = document.getelementbyid("list"); var add = document.getelementbyid('addelem'); add.addeventlistener('click', function() { var itemsbytagname = document.getelementsbytagname("li"); list.innerhtml += '<li>item ' + i++ + '</li>' });
<ul id="list"> <li>item </li> </ul> <button id="addelem">add</button>
Comments
Post a Comment