JavaScript Why can't I create html element dynamically? canvas,canada,cancelled,canceled vs cancelled,cancun,canada goose,cane

I want to create paragraph dynamic,so I wrote the following code. After user click the button, a new paragraph whose innerText is the user's inputs should be added.But I failed, Is there someone can help me?

var input = document.querySelector(".input");
var btn = document.querySelector("button");
var body = document.querySelector("body");

btn.addEventListener('click', addParagraph);

function addParagraph() {
  var childELes = body.children;
  for (var p in childELes) {
    if (p.tagName === "p")
      p.remove();
  }
  if (input.value.trim() != "") {
    var newPara = document.createElement("p");
    newPara.innerText = input.value;
    body.appendChild(newPara);
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>test</title>
</head>

<body>
  <form>
    <p>
      <label>input:</label>
      <input type="text" class="input">
      <button>add</button>
    </p>
  </form>
</body>

</html>
Answer:1

Your code is working but the form is being submitted that is why the dynamically added value get lost and page is refreshed. To make it work as you expect, you need to use e.preventDefault(); to prevent the form from being submitted:

var input = document.querySelector(".input");
var btn = document.querySelector("button");
var body = document.querySelector("body");

btn.addEventListener('click', addParagraph);

function addParagraph(e) {
  e.preventDefault();
  var childELes = body.children;
  for (var p in childELes) {
    if (p.tagName === "p")
      p.remove();
  }
  if (input.value.trim() != "") {
    var newPara = document.createElement("p");
    newPara.innerText = input.value;
    body.appendChild(newPara);
  }
}
<form>
  <p>
    <label>input:</label>
    <input type="text" class="input">
    <button>add</button>
  </p>
</form>
Answer:2

Add type="button" to your button html tag:

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
    <form>
        <p>
            <label>input:</label>
            <input type="text" class="input">
            <button type="button">add</button>
        </p>
    </form>
    <script type="text/javascript">
    var input = document.querySelector(".input");
    var btn = document.querySelector("button");
    var body = document.querySelector("body");

    btn.addEventListener('click', addParagraph);

    function addParagraph() {
        var childELes = body.children;
        for (var p in childELes) {
            if (p.tagName === "p")
                p.remove();
        }
        if (input.value.trim() != "") {
            var newPara = document.createElement("p");
            newPara.innerText = input.value;
            body.appendChild(newPara);
        }
    }
    </script>
</body>
</html>
Answer:3

You can just add .preventDefault() to prevent the button from refreshing the page.

var input = document.querySelector(".input");
var btn = document.querySelector("button");
var body = document.querySelector("body");

btn.addEventListener('click', addParagraph);

function addParagraph(e) {
	e.preventDefault();
  var childELes = body.children;
  for (var p in childELes) {
    if (p.tagName === "p")
      p.remove();
  }
  if (input.value.trim() != "") {
    var newPara = document.createElement("p");
    newPara.innerText = input.value;
    body.appendChild(newPara);
  }
}
<!DOCTYPE html>
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <form>
      <p>
        <label>input:</label>
        <input type="text" class="input">
        <button>add</button>
      </p>
    </form>
  </body>
</html>
Answer:4

It's because the button in form invokes a submit and reloads the page. You see the new p-tag for short time. But the page reloads and all p a gone.

Add type="button" to your button to exclude it as submit button or use event.preventDefault() in your button event.

Answer:5

Your code is 100% correct the only problem happening is your <button>add</button> is doing a postback which is refreshing your page.

You just need to add a type of button to your button tag and it will work like this:

<button type="button">add</button>

Hope this helps

Answer:6

I'm trying to create a news style marquee at the bottom of my XSL page that shows some information it retrieves from an XML file. What I want it to do is automatically refresh the content of only this ...

I'm trying to create a news style marquee at the bottom of my XSL page that shows some information it retrieves from an XML file. What I want it to do is automatically refresh the content of only this ...

  1. reload iframe content using javascript
  2. reload div content using javascript
  3. reload div content using ajax
  4. reload a div content using jquery
  5. reload content using javascript
  6. reload content using ajax
  7. javascript reload iframe content
  8. reload iframe from javascript
  9. how to reload iframe in javascript

I have an array of letters and numbers. let sortLetters = [ 'R', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1' ] I want to sort the types alphabetically, then return the first letter I come ...

I have an array of letters and numbers. let sortLetters = [ 'R', '1', 'U', '1', 'N', '1', 'D', '1', 'M', '1', 'C', '1' ] I want to sort the types alphabetically, then return the first letter I come ...

Hello I am taking an array of integers with ranging numbers from 1 - 100 and I'm counting the duplicated numbers within it. Example, array[1,1,1,1,1,100,3,5,2,5,2,23,23,23,23,23,]. Result = 1 - 5 ...

Hello I am taking an array of integers with ranging numbers from 1 - 100 and I'm counting the duplicated numbers within it. Example, array[1,1,1,1,1,100,3,5,2,5,2,23,23,23,23,23,]. Result = 1 - 5 ...

  1. find duplicate count in excel
  2. find duplicate count in sql
  3. find duplicate count in list java
  4. find duplicate count in array
  5. find duplicate count in list c#
  6. find duplicate count in java
  7. find duplicate count in oracle
  8. count find duplicate rows
  9. find count of duplicate rows in sql
  10. find count of duplicate values in excel
  11. find count of duplicate values in list java
  12. find count of duplicate number in array
  13. find count of duplicate values in sql
  14. excel find duplicate count if
  15. how to find duplicate count in array in java
  16. how to find duplicate count in arraylist
  17. how to find duplicate count in excel using formula
  18. how to find duplicate count in mysql
  19. formula to find duplicate count in excel

I want to create a method to delete null and undefined attributes. To do that : I convert my plain object into table loop on it filter on attributes if there are null or undefined ...

I want to create a method to delete null and undefined attributes. To do that : I convert my plain object into table loop on it filter on attributes if there are null or undefined ...

  1. javascript delete null undefined