I am trying to make a little game thing where you can enter words and such, but the main issue is that when I try to make JavaScript print out the words I write; it doesn't ever work properly! Here is my HTML:
<input id="verb" name="verb" type="text" placeholder="Enter a verb">
<input id="noun" name="noun" type="text" placeholder="Enter a noun">
<button onclick="getWords()">Enter</button>
<p id="output"></p>
And here's the JavaScript to go with it:
var verb = document.getElementById("verb").textContent;
var noun = document.getElementById("noun").textContent;
function getWords()
{
document.getElementById("output").textContent = "You chose " +verb+ " as your verb, and " +noun+ " as your noun";
}
When you click the enter button in Google Chrome; it simply says that "You chose undefined as your verb, and undefined as your noun."
Why doesn't it recognize the contents of the elements and just fill them with "undefined," no matter what is entered? Is there an easier way to do this? Thanks in advance!
verbandnounaccessions to the inside of yourgetWordsfunction.document.getElementById("id").val();anddocument.getElementById("id").val("your value");instead.val()function. Thinking of jQuery perhaps?