palak sharma palak sharma - 2 months ago 12
jQuery Question

how to add a line break or <br/> after every list item using jquery?

I have 100 list items and to make it look nicer i want to add a line break tag after the end of every list item. So how can I do it using javascript or jquery?

Answer

Here is a simple solution, I used insertAfter (jquery) to insert <br/> after each li

$(function(){

  $("<br/>").insertAfter("li")
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>ITEM 1</li>  
  <li>ITEM 2</li>  
  <li>ITEM 3</li>  
  <li>ITEM 4</li>  
</ul>

But, you don't need JavaScript for doing this, you could do this easily by CSS

ul > li{
  margin-top:25px;
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
</ul>

Comments