Some problems there:
-
You’ve defined a function, but you never call it, so the code inside it never runs.
-
If you ran it from the script where you have the
script
tag, it would fail because the elements wouldn’t exist yet. -
”
is not the correct quote for an HTML attribute."
is.
If you move your script
tag to the end of the body, just before the closing </body>
tag, fix the quotes, and you call the function, it works:
function myFunction() {
var newElement = document.createElement("li");
newElement.textContent = "I am new";
var ol = document.getElementById("fruits");
ol.appendChild(newElement);
}
myFunction();
<h1>Fruit Self</h1>
<ol id="fruits"></ol>
Pick a fruit:
<input type="text" id="newfruit">
<h1>Basket</h1>
<ol id="basket"></ol>
As a non-snippet for clarity:
<!DOCTYPE html>
<html>
<head>
<title>Hi</title>
</head>
<body>
<h1>Fruit Self</h1>
<ol id="fruits"></ol>
Pick a fruit:
<input type="text" id="newfruit">
<h1>Basket</h1>
<ol id="basket"></ol>
<script type="text/javascript" src="https://stackoverflow.com/questions/40073169/script.js"></script>
</body>
</html>
and
function myFunction() {
var newElement = document.createElement("li");
newElement.textContent = "I am new";
var ol = document.getElementById("fruits");
ol.appendChild(newElement);
}
myFunction();
Side note: You can and should leave the type
off your script
tag. The default is JavaScript.