Home > OS >  Adding an <a> tag to a list with jQuery
Adding an <a> tag to a list with jQuery

Time:12-24

I am trying to add an <a> tag to a list of li with the help of jQuery.

The html looks like this:

<div >
      <ul >
      <li ></li>
      <li ></li>
      <li ></li>
      </ul>
    </div>

I created an empty <a> tag with jQuery. Unfortunately I can't find a way to add this <a> tag to all my <li> (I tried the jQuery append method).

 let aTag = $("a");
     $(".my list").append(aTag);

I am new to coding, so I am sure I am doing a silly mistake here. Thanks in advance.

CodePudding user response:

$('.my.list').append('<a>') is all you need:

$('.my.list').append('<a>')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div >
  <ul >
    <li ></li>
    <li ></li>
    <li ></li>
  </ul>
</div>

Since your list items have two classes you need to select them with $('.my.list')

CodePudding user response:

You can try:

$(".my list").html("<a href='#'>This is a link</a>")
  • Related