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

188
Visualizações
why do the (ndev) variable not added to the added field class

why do the (ndev) variable not added to the added field class? I am trying to append the ndev var to the addedfield class when the user clicks on the button ... what is the problem here? js code

//1>>>  first step is to creat div with the value which the user add
//get the value
const inputvalue=document.querySelector("input").value;
//create element
const div =document.createElement("div");
//adding the value and the button to it
const ndev=div.innerHTML=`${inputvalue}<input type="submit" value="delete" >`;
//2>>> style this div
div.classList.add("styleddiv");

const nst =document.querySelector(".styleddiv")


//add event listener
document.getElementById("button").addEventListener("click",
function() {
    document.getElementsByClassName("addedfield").appendChild(ndev);
    
    }
);

HTML code

<!DOCTYPE html>
<html lang="en">
<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>
    <link rel="styleSheet" href="../cssjsfirst practice.css">
</head>
<body>
<div class="container">

   <form action="">
    <input type="text" placeholder="type a task" autofocus>
    <input type="submit" value="Add Task" id="button">

   </form>

<div class="addedfield">
</div>
</div>
<script src="first js practice.js"></script>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Errors

  1. document.querySelector("input") is not precise anough, you should probably add class of "input" to your first input of type "text".

  2. getElementsByClassName returns an array of HTML elements. You should try to change it to getElementsByClassName[0]

  3. It's good idea to prevent refreshing page after clicking submit by adding onsubmit="return false" to your HTML form element.

  4. This line:

const ndev=div.innerHTML=`${inputvalue}<input type="submit" value="delete" >`;

doesn't actually create HTML element.

  1. Part of your Javascript code:
    const inputvalue=document.querySelector("input").value;
    //create element
    const div =document.createElement("div");
    //adding the value and the button to it
    const ndev=div.innerHTML=`${inputvalue}<input type="submit" value="delete" >`;
    //2>>> style this div
    div.classList.add("styleddiv");

is executed only one time, just after you load the script. It would be better to move this functionality to eventListener function, so you get the user's input after he presses the button, and not on page load.

Possible answer

Please take a look on this version of HTML and JS:

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<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>
    <link rel="styleSheet" href="../cssjsfirst practice.css">
</head>
<body>
<div class="container">

   <form action="" onsubmit="return false">
    <input class="input" type="text" placeholder="type a task" autofocus>
    <input type="submit" value="Add Task" id="button">
   </form>

<div class="addedfield">
</div>
</div>
<script src="practice.js"></script>

</body>
</html>
// practice.js
    document.getElementById("button").addEventListener("click",
    function() {
    const inputvalue=document.querySelector(".input").value;
    const div =document.createElement("div");
    div.innerHTML=`${inputvalue}<input type="submit" value="delete" >`;
    div.classList.add("styleddiv");
    document.getElementsByClassName("addedfield")[0].appendChild(div);
    }
);

as you can see, only after the user clicks button, we assign the value of HTML element of class "input" to inputvalue, and then we create the "div" element, and fill it with innerHtml of your choice.

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