How to insert a LI into a UL with Javascript?

Asked

Viewed 1,544 times

-3

I’m looking to add a LI between two LI’s of a UL. I don’t have access to the CSS/HTML code of the website template, if it would not create easily, so I’m thinking about adding via html and javascript.

I’ve tried everything and I couldn’t, someone would have an idea of how to accomplish this?

wanted to insert this LI

<li>
    <a href="https://www.xyz.com.br/" title="XYZ">
        <img src="https://xyz.com.br/xyz.png" alt="xyz" width="200px">
    </a>
</li>

Follow the code...

<div class="atalhos-mobile visible-phone fundo-secundario borda-principal" style="background-color: rgb(255, 0, 128); border-bottom: 1px solid rgba(44, 43, 43, 0.18);">

<ul>

    <li class="menu-mobile-botao">
        <button type="button" class="navbar-toggle" data-toggle="slide-collapse" data-target="#slide-collapse" aria-expanded="false">
            <svg class="icone-menu" style="fill: rgb(255, 213, 65);">
                <use xlink:href="#icone-menu"></use>
            </svg>
        </button>
    </li>

    **//////////// QUERIA COLOCAR A LI AQUI ////////////////////////////**

    <li class="home-mobile">
        <a href="https://www.xyz.com.br/" class="">
            <svg class="icone-home" style="fill: rgb(255, 255, 255);">
                <use xlink:href="#icone-home"></use>
            </svg>
        </a>
    </li>

</ul>
</div>

2 answers

0

1º Put an id in ul
<ul id="lista">

2º Use the function below in some example event

<button onclick="adicionar()">Adicionar linha</button>

function adicionarLi() {
  var lista  = document.getElementById("lista").innerHTML;
  lista = lista +"<li>"+seuTextoAqui+"</li>";
  document.getElementById("lista").innerHTML = lista;
}
  • 1

    it would not be possible to solve the problem just by using: document.getElementById("lista").innerHTML += "<li>"+seuTextoAqui+"</li>" within the add function?

  • 1

    Nor would it be more interesting to use querySelector?

  • I don’t have access to the theme/template to give ID to UL. If not easily. This being the bottleneck. but thanks for trying to help

0

I will try to help using such example

<h2>frutas</h2>

<input type="text" id="frutas" />

<button onclick="adicionar()">Adicionar frutas</button>

<br>

<ul id="lista">
  <li>banana</li>
  <li>morango</li>
  <li>manga</li>
  <li>uva</li>
</ul>

Now, the Javascript for the function add

function adicionar() {
  var fru = document.getElementById("frutas").value;
  var lista  = document.getElementById("lista").innerHTML;
  lista = lista +"<li>"+fru+"</li>";

  document.getElementById("lista").innerHTML = lista;
}
  • thanks friend, I understood. but actually I wanted the element to be between 2 LI. In the onload of the page. It was an element of the site. I even made a new post for examplelook better https://answall.com/questions/414641/como-addir-um-li-em-uma-ul-atrav%C3%A9s-de-javascript

Browser other questions tagged

You are not signed in. Login or sign up in order to post.