You can do it this way:
function insereLi(texto)
{
// Localiza o elemento <ul> através do seu id, usando getElementById().
var minhaLista = document.getElementById('minhaListaDesordenada');
// Cria um novo elemento <li> no documento.
var newLi = document.createElement('li');
// Informa o texto do novo elemento <li>.
newLi.textContent = texto;
// Insere o novo elemento <li> na lista <ul>.
minhaLista.appendChild(newLi);
}
function insereLiComLink(url, descricao)
{
// Localiza o elemento <ul> através do seu id, usando querySelector().
var minhaLista = document.querySelector('#minhaListaDesordenada');
// Cria um novo elemento <li> no documento.
var newLi = document.createElement('li');
// Informa o texto do novo elemento <li>.
newLi.innerHTML = '<a href ="' + url + '" target = "_blank">' + descricao + '</a>';
// Insere o novo elemento <li> na lista <ul>.
minhaLista.appendChild(newLi);
}
// Testa as duas funções criando mais 3 elementos <li> à lista <ul>.
insereLi('Item dinâmico 1')
insereLiComLink('https://www.jetbrains.com/pycharm/', 'PyCharm')
insereLi('Item dinâmico 3')
<ul id="minhaListaDesordenada">
<li>Item fixo</li>
</ul>
The first function, insereLi()
, inserts an element <li>
only with text, and she uses the method document.getElementById()
(documentation) to find the list <ul>
by your id (which should be unique), and then use the property textContent
(documentation) of the new element <li>
, since we are only inserting text.
Already the second function, insereLiComLink()
, receives a URL and a description to mount an element <a>
within the element <li>
. In this role I used the document.querySelector()
(documentation) to find the list <ul>
by your id, to show an alternative. Here I used the property innerHTML
(documentation) to add the element <a>
within the new element <li>
.
To isac response shows how to add the element <a>
at the <li>
using document.createElement()
and document.createTextNode
.
For the research I’ve done (What Is A Text Node, Its Uses?), because I had this doubt and the answer from Isac made me think about it, some differences between using element.innerHTML
and document.createTextNode()
+element.appendChild(textNode)
sane:
- The estate
innerHTML
is more recent and the "old" way was to use the createTextNode()
;
- That is why the method
createTextNode()
may be more compatible with most browsers;
- I thought using the method
createTextNode()
would be more costly to create a new object, but what the property innerHTML
in the background is to analyze (parse) HTML and create the necessary nodes, including text nodes, because the GIFT (Document Object Model) "organizes" all its contents as branches of a tree, ending in us, and each node contains objects, so at the bottom, objects will be created anyway, even for a simple text within a tag;
- Precisely because of the fact of the property
innerHTML
analyze (parse) the HTML, the use of it can cause a security risk, if incoming user input values are used to populate the property, because it can be used for script injection.
I was wondering why use
document.createTextNode()
instead of simply using the propertyinnerHTML
, since it seems more laborious and even more costly thinking logically (more a created object), and I have now seen this answer: Is there any major Difference between innerHTML and using createTextNode to Fill a span?. I had thought to put in my reply a version that also added the tag<a>
using the methodcreateElement()
, but I didn’t, and you answered almost together and did so. I voted in your reply! ;-)– Pedro Gaspar
@Pedrogaspar innerHTML is of course simpler but I chose to keep the style that AP had. Yet there is one or two subtle differences that may or may not be relevant, such as JS injection.
– Isac
Oh yes, I also thought of using the
createTextNode
to follow the same style, only I chose to show a different way, however I wanted to show the form withcreateTextNode
also, but fortunately you have met that need! I can add this information about the difference between the two in my reply or you will add in your?– Pedro Gaspar
@Pedrogaspar Just as I later thought I’d put an alternative with
innerHTML
but then when I noticed there was already your answer with this alternative :D. Feel free to put the differences, which is something that enriches the answer.– Isac
Good morning @Isac. I successfully implemented your collaboration on my first study page. https://maxwneto.github.io/ Thanks for your help.
– Max Wilson