# Dynamic HTML element in JS not persisting

**URL:** <https://forum.freecodecamp.org/t/dynamic-html-element-in-js-not-persisting/708907>\
**Category:** General\
**Created:** [August 31, 2024, 12:57pm UTC](https://forum.freecodecamp.org/t/dynamic-html-element-in-js-not-persisting/708907 "2024-08-31T12:57:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwink08](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwink08/32/365477_2.png) [@jwink08](https://forum.freecodecamp.org/u/jwink08)\
**Post date:** [August 31, 2024, 12:57pm UTC](https://forum.freecodecamp.org/t/dynamic-html-element-in-js-not-persisting/708907/1 "2024-08-31T12:57:46Z")

</div>

I am having issues where I am creating an HTML element in Javascript and appending it to an element in the DOM. I am seeing the element on the page very briefly. Once the page loads the element disappears. Could I get help on where I need to go in debugging this issue?

I have also tried creating a separate function that handles updating and creating the element that gets added to the output element, and calling that function from the event listener. Both methods end with the same result where the element will get created and briefly flashes on the page but disappears once element is finished loading.

```auto
const button = document.getElementById('convert-btn');
const number = document.getElementById('number');
const output = document.getElementById('output');

button.addEventListener('click', (e) => {

    if(number.value === ''){
        let result = document.createElement('p');
        let node = document.createTextNode('Please enter a valid number');
        result.appendChild(node);
        output.appendChild(result);
       
    }
});

```

Here is my HTML:

```auto
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Jakes Roman Numeral Converter</title>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <link rel='stylesheet' type='text/css' media='screen' href='styles.css'>
   
</head>
<body>
    <h2>Jake's Roman Numberal Converter</h2>
    <form id="form">
        <fieldset id="form">
            <h3 id="form-title">Enter Number:</h3>
            <input type="number" id="number">
            <button id="convert-btn">convert</button>
        </fieldset>
    </form>     
    <div id="output"></div>
    <script src='script.js'></script>
</body>
</html>

```

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [August 31, 2024, 1:29pm UTC](https://forum.freecodecamp.org/t/dynamic-html-element-in-js-not-persisting/708907/2 "2024-08-31T13:29:13Z")

</div>

Try calling the event.preventDefault function to stop the page from reloading on the button click.

Example code:

```js
document.getElementById("myb").addEventListener("click", function(event){
  event.preventDefault()
});

```

---

<div class="post-metadata">

**Author:** ![jwink08](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwink08/32/365477_2.png) [@jwink08](https://forum.freecodecamp.org/u/jwink08)\
**Post date:** [August 31, 2024, 2:09pm UTC](https://forum.freecodecamp.org/t/dynamic-html-element-in-js-not-persisting/708907/3 "2024-08-31T14:09:42Z")

</div>

Wow!! that worked. Thanks @hbar1st. I want to ask since I am not sure I fully understand what is going on here. Does a on “click” event always by default reload the page, and thats why I wasn’t seeing my element appear on the page?

So calling `preventDefault()` stops this from occurring? Any change you have any references that documents best practices around eventListeners? When using a click event should I always call `preventDefault()`?

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [August 31, 2024, 2:37pm UTC](https://forum.freecodecamp.org/t/dynamic-html-element-in-js-not-persisting/708907/4 "2024-08-31T14:37:22Z")

</div>

because your button element is not defined as type=“button” it will be assumed by the browser that this button is a “submit” type of button. A submit button will always try to ‘submit’ the contents of the associated form and this causes the refresh.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:32pm UTC](https://forum.freecodecamp.org/t/dynamic-html-element-in-js-not-persisting/708907/5 "2025-01-08T15:32:00Z")

</div>

Closed on January 8, 2025, 3:32pm UTC


