<body> <p id="demo"> <li>1</li> <li>1</li> <li>1</li> <li>1</li> <li>1</li> <li>1</li> <li>1</li> <li>1</li> </p> </body> <script> var num = document.getElementById('demo'); var lis = document.getElementsByTagName('li').length; for (var i = 0; i < lis.length; i++) { if (i>4) { document.getElementsByTagName('li')[i].style.display = "none"; } } </script>