wlh wlh - 4 months ago 19
HTML Question

Why do my DOM changes disappear when I click a form button?

This is my first post, so consider me a n00b.

I have created a simple form to access a value stored in a javascript array. I am trying to append an element to display the proper value. The logic works, my result will show, but only briefly, before it is overwritten. Can you help?


<input type="text" label = "sitex" id="site"><br>
<input type = "submit" value = "Submit" onclick="getFormData(document.getElementById('site').value)">

<div id = "result"><p>Results:</p></div>

<script type="text/javascript" src="logic.js"></script>


var results = "";
function getFormData(val1) {
var pw = {
"site1": "xyzabc",
"site2": "defghi",
"site3": "jklmno",
"site4": "pqrstu",
"site5": ["id1", "vwxyza"],
"site6": ["id2", "bcdefg"],
"site7": "hijklm",
"site8": ["id3", "nopqrs"],
"site8": ["id4", "tuvwxy"],
"site9": ["id5", "zabcde"],
"site10": "fghijk",
"site11": ["id6", "lmnopq"],
"site12": "rstuvw"
results = pw[val1];

function showResults(val2) {
var div = document.createElement('div');
var pss = document.createTextNode(val2);
div.style.color = "red";


You're actually submitting your form when you click the Submit button and page is reloading. Just change the type of button from submit to button:

<input type="button" value="Submit" onclick="getFormData(document.getElementById('site').value)">