More than one value for the same appendchild()

Asked

Viewed 198 times

0

How can I concatenate two values in the same appendchild?

For example, transform this

li.appendChild(_nomeAutorTexto);
li.appendChild(_sobrenomeAutorTexto);

therein

li.appendChild(_nomeAutorTexto + espaço + _sobrenomeAutorTexto);

Here the whole code:

const listaTextos = document.querySelector("#lista-textos");

function renderLista (doc) {

    let li = document.createElement("li");
    let _nomeAutorTexto = document.createElement("span");
    let _sobrenomeAutorTexto = document.createElement("span");
    let _nomeTexto = document.createElement("span");
    let _livroTexto = document.createElement("span");

    li.setAttribute("data-id", doc.id);
    _nomeAutorTexto.textContent = doc.data().nomeAutorTexto;
    _sobrenomeAutorTexto.textContent = doc.data().sobrenomeAutorTexto;
    _nomeTexto.textContent = doc.data().nomeTexto;
    _livroTexto.textContent = doc.data().livroTexto;

    li.appendChild(_nomeAutorTexto);
    li.appendChild(_sobrenomeAutorTexto);
    li.appendChild(_nomeTexto);
    li.appendChild(_livroTexto);

    listaTextos.appendChild(li);

}

This is a simple matter of syntax or do I have to do some more sophisticated trick?

  • If you have all the code?

  • 1

    Hi Virgilio: I just put :-)

  • For example: if you put only one text in Li I think you don’t need to create two spam or this data will be formatted with css or are they just texts? or is there something to take this information to develop something.?

  • The end, what is your goal? From what I understand, your code is working. You just want to summarize these lines of appendChild()?

  • 1

    See your doubt seems not to have, because I’m not seeing problems in this code, just can’t concatenate elements.

  • @Virgilionovic, the code works, I just wish the messages _name Authorext and _name Authorext were on the same line ...

  • Create the values in an element! why? o appendChild only accepts one element at a time it works like this, it was done like this. the maximum of merging the two information into only one element.

  • That’s right, @Lipespry: I’d like the links _Name Authorext and _Name Authorextext to be in the same appendchild() ...

  • In that case, my answer is valid again. It cannot. Of course you can get the same result of its function in numerous ways, but it has no relation to your question. As @Virgilionovic mentioned: you can put everything into a single element. Only it will change the final result. li will have a single span with all texts "concatenated".

  • Whoa, coming to light! I could concatenate the information in that part of the code, so that the content of _nameAuthorText includes tb _last nameAuthorText? _nameAuthoritText.textContent = (doc.data().nameAuthoritText + oc.date().last nameAuthoritText) ?

  • 1

    That’s exactly what @Virgilionovic suggested: "- Create values in an element!" Note only that you change the final result. As he himself mentioned: "- If you are going to put only one text in Li do you think you do not need to create two spam or these data will be formatted with css or are only text even? or is there something to take this information to develop something.?"

  • 1

    Thanks, people, I will study a little more to implode the suggestion of @Virgilionovic. Gratefully!

  • If solved, the solution should be in the form of an answer, not an edition of the question;

  • Thanks for the touch, @Andersoncarloswoss.

Show 9 more comments

4 answers

1

Well, I think it’s been explained that appendChild accepts only one argument, and concatenating objects also does not work, because any object concatenation results in a string, which does not serve as a parameter for appendChild.

However, there is the option to create a function, or a method that does what you want. You can even extend the class Node. For example, if we add the method appendChildren:

Object.defineProperty(Node.prototype, 'appendChildren', {
    configurable: true,
    value: function(...children) {
        for (const child of children) {
            this.appendChild(child);
        }

        return this;
    }
});

We can do what we want now using this method:

li.appendChildren(_nomeAutorTexto, _sobrenomeAutorTexto, _nomeTexto, _livroTexto);

0

The solution that served me was the following:

1) I have created spam for first and last name:

let _nomeCompletoAutorTexto = document.createElement("span")

2) The content of this spam is the concatenation of two BD fields:

_nomeCompletoAutorTexto.textContent = doc.data().nomeAutorTexto + ' ' + doc.data().sobrenomeAutorTexto;

3) I have created a new appendchild() for these:

li.appendChild(_nomeCompletoAutorTexto);

4) Happy ending.

Thank you all!

0

"- How can I concatenate two values in the same appendchild?"

It cannot. It is not the correct use of the method.

According to the documentation, this is the method syntax:

node.appendChild(node)

Where node are the "we" of objects.

This method expects a single object to be rendered/moved to after the last child of the object in question.

Following his example, _nomeAutorTexto and _sobrenomeAutorTexto are object nodes. Therefore, you cannot concatenate directly into the method call expecting it to render/move both objects.

Recommended reading: HTML DOM appendchild() Method

0

It follows a way to implement if I understand the question correctly:

var li = document.createElement('LI');           // cria uma tag li
var span1 = document.createElement('span');      // cria a primeira tag span
span1.textContent = 'TEXTO 02';                  // seta um texto na span 01
var span2 = document.createElement('span');      // cria a segunda tag span
span2.textContent = ' TEXTO 03';                 // seta um texto na span 02
var lista = document.getElementById('lista');    // referencia a ul

li.appendChild(span1);                           // adiciona a span 01 no final da ul
li.appendChild(span2);                           // adiciona a span 02 no final da span 01

lista.appendChild(li);                           // adiciona os textos na ul
<ul id="lista">
  <li>TEXTO 01</li>
</ul>

Browser other questions tagged

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