My HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Competition</title>
<link href="css/style.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Noto+Sans+JP|Pacifico&display=swap" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<header>
<h1>Football</h1>
<p>A Fottball Competition App</p>
<form id="registrar">
<input type="text" id="txtVal" name="name" placeholder="Invite Team">
<button type="submit" onclick="addLi()" name="submit" value="submit">Submit</button>
</form>
</header>
<div class="main">
<h2>Invitees</h2>
<ul id="invitedList">
</ul>
</div>
</div>
<script type="text/javascript" src="javascript/app.js"></script>
</body>
</html>
I have to add JavaScript, so that when you enter a sting into the input field and click submit it adds a <li> element to the <ul>.
I've tried using the following script:
function addLi()
{
var txtVal = document.getElementById('txtVal').value,
listNode = document.getElementById('invitedList'),
liNode = document.createElement("LI"),
txtNode = document.createTextNode(txtVal);
liNode.appendChild(txtNode);
listNode.appendChild(liNode);
}
But because the button has the submit type it reloads the page and the <li> disappears.
Is there a way to do this without editing the HTML?
buttoninstead ofsubmitaddLifunction?