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

138
Visualizações
Accessing an element in a form that has no id in javascript

I have an assignment as a part of a cyber security course where I am supposed to submit a form with specific values for the inputs.

here is the relevant snippet of the source code I am working with is: (source code for a messaging system)

 <div class="form-group">
    <label for="inputSubject">Subject:</label>
    <input name="subject" type="text" class="form-control" id="inputSubject" placeholder="Enter a subject" pattern=".{5,}" required>
</div>
<div class="form-group">
    <textarea name="contents" class="form-control" rows="10" placeholder="Enter your message" pattern=".{5,}" required></textarea>
</div>

whenever I am trying to set the value of the textarea using either getElementsbyName or getElemetnsbyTagName, I get the error "Uncaught TypeError: Cannot set properties of null. Any idea how to fix this?

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

0

Two things:

  1. Ensure you are correctly referencing the element
  2. Set with either value or textContent.
const textArea = document.querySelector('textarea[name="contents"]');
textArea.textContent = `whatever you want it to say`.
about 4 years ago · Juan Pablo Isaza Relatório

0

As a getElementsByName return a list of nodes and you only have one element with that name:

This should work for you:

document.getElementsByName("contents")[0].value = "hello world"

here how you could implemented:

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="form-group">
        <label for="inputSubject">Subject:</label>
        <input name="subject" type="text" class="form-control" id="inputSubject" placeholder="Enter a subject"
            pattern=".{5,}" required>
    </div>
    <div class="form-group">
        <textarea name="contents" class="form-control" rows="10" placeholder="Enter your message" pattern=".{5,}"
            required></textarea>
    </div>

    <script>
        document.getElementsByName('contents')[0].value = 'hello world'
    </script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza Relatório

0

Import a script in your html with this way <script defer src="script.js"></script> so that the DOM tree is created. In your imported script, you can do this: document.getElementsByTagName("textarea")[0].value = "Test Value";

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