0

I am creating a poem with blank lines. The blank must be changed and I have javascript created to check if the word that is entered is the correct word that should be in the poem.

Anyone know what I am doing wrong?

<body>
<pre id="poem">
Social Media
Relax yourself,
As I <span id = "word1" contenteditable>______</span>
through your mind
Scroll down the pages
of your spine
Reading every word 
and thought on
your <span contenteditable>____</span> like a <span contenteditable>____</span>
Stumbled Upon 
you then <span contenteditable>_______</span> onto
your looks--IGuess
I'm <span contenteditable>______</span> into you
You're my one
and only <span contenteditable>________</span>

Will you <span contenteditable>______</span> me?

</pre>
<button onsubmit = "isCorrect()"> submit </button>
<p color = "white" id = "demo"></p>

</body>

Javascript:

<script>
function isCorrect(){
    var word = document.getElementById("word1").value;
    if (word == "dig")
    {
        document.getElementById("demo").innerHTML = word;
    }
}
</script>
StevenR
  • 435
  • 1
  • 6
  • 16

1 Answers1

0

word1 is a span, use innerHTML instead of value. And I would recommend addEventListener instead of onsubmit.

document.querySelector( "button" ).addEventListener( "click", function(){
   isCorrect();
});
function isCorrect(){
    var word = document.getElementById("word1").innerHTML;
    if (word == "dig")
    {
        document.getElementById("demo").innerHTML = word;
    }
}

Similarly, you can check the values of other spans as well. I would recommend putting the values as the attribute of span itself, for example

As I <span id = "word1" data-value="dig" contenteditable>______</span>

And verify the same as

var allSpans = document.querySelectorAll( span[contenteditable] );
Array.from(allSpans).forEach( s => {
   var dataValue = s.getAttribute( "data-value" );
   if (dataValue == s.innerHTML )
   {
     //code for correct value
   }
})
gurvinder372
  • 66,980
  • 10
  • 72
  • 94