Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
How to get an span from its textnode or text content?

I have a form with 3 fields:

            <form id="book-form">
            <div class="form-group">
              <label for="title">Title</label>
              <input type="text" name="title" class="form-control" placeholder="Enter a title">
            </div>
            <div class="form-group">
              <label for="author">Author</label>
              <input type="text" name="author" class="form-control" placeholder="Enter the author of the book">
            </div>
            <div class="form-group">
              <label for="isbn">ISBN#</label>
              <input type="text" name="isbn" class="form-control" placeholder="Enter the book isbn">
            </div>
            <button type="submit" class="btn btn-primary btn-block">Add Book to store</button>
        </form>

Here are I am retrieving the value of these fields that I will insert in their respective span in the html.

const title = document.getElementsByName('title')[0].value
const author = document.getElementsByName('author')[0].value
const isbn = document.getElementsByName('isbn')[0].value

Now I have three span tags where the value of these form fields are suppose to be inserted.

<span class="title">// the value of title</span>
<span class="author">// the value of author</span>
<span class="isbn">// the value of isbn</span>

Now I have a function that checks if the retrieve from the fields of the form is not empty(null) if that is the case I want to remove the span that is was suppose to be in the dom.

function insertMe(fieldValue) {
if (fieldValue === "") {
    // How to remove the span that it was suppose to go
} else {
    return fieldValue
}

}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's not clear how you're calling insertMe, and the name of that function is misleading because you're only removing elements, not adding them.

I'd approach it this way.

When the button is clicked/onSubmit call the function and use querySelectorAll to target all the inputs by class. Iterate over them and if the value is an empty string remove the span whose class matches the name of the input, otherwise set the text content of the span to the input value.

const button = document.querySelector('button');
button.addEventListener('click', handleClick, false);

function handleClick() {
  const inputs = document.querySelectorAll('.form-control');
  inputs.forEach(({ name, value }) => {
    const el = document.querySelector(`span.${name}`);
    if (el && !value) {
      el.remove();
    } else {
      el.textContent = value;
    }
  });
  
}
<input type="text" class="form-control" name="title" placeholder="Enter a title">
<input type="text" class="form-control" name="author" placeholder="Enter an author">
<input type="text" class="form-control" name="isbn" placeholder="Enter an ISBN number">
<button>Click</button>
<br/><br/>
<span class="title">Title</span><br/>
<span class="author">Author</span><br/>
<span class="isbn">ISBN</span><br/>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda