0
function countWords(){
    var sentence = document.getElementById("sentence-input").value;

    var text = sentence.split(" ");

    var lengthofString = text.length;

    document.getElementsByClassName("word-count").innerText = lengthofString;

    console.log(text);
    console.log("length of string " + lengthofString);

}

Here is my current code, I'm able to get the length of the current string but i dont know how to simply display that number in my paragraph element.

Yahya
  • 13
  • 2
  • 1
    `getElementsByClassName` returns collection, use index `document.getElementsByClassName("word-count")[0].innerText = lengthofString;` – Mamun Apr 23 '21 at 10:29
  • I suggest `document.querySelector(".word-count").innerText = lengthofString;` - it makes more sense when there is only one element, or use an ID there too – mplungjan Apr 23 '21 at 10:37
  • Thank you guys i tried your suggestions and was able to figure it out! – Yahya Apr 23 '21 at 11:02

0 Answers0