0

Can anybody tell me why this click event won't trigger? The function seems to work fine (except printing out to HTML but I'll come back to that).

function longestWordFunc(stringArg) {
  console.log(stringArg)
  var stringSplit = stringArg.split(" ");
  console.log(stringSplit)
  var longestWord = 0;
  
  for(var i = 0; i < stringSplit.length; i++){
    if(stringSplit[i].length > longestWord){

      longestWord = stringSplit[i].length;   
    }
    longestWord = stringSplit[i];
   };
  console.log(longestWord)
  console.log(longestWord.length)
  document.getElementsByClassName("longestWord").innerHTML = longestWord;
  document.getElementsByClassName("longestWordCount").innerHTML = longestWord.length;
};

let searchString = document.querySelector(".searchString").value;
console.log(searchString);
document.querySelector(".generate").addEventListener("click", longestWordFunc(searchString));
<input type="text" name="searchString" class="searchString">

<span class="longestWord"></span>
<span class="longestWordCount"></span>

<button class="generate">Generate</button>

Thank you in advance.

2 Answers 2

2

You should define searchString inside the event listener of .generate. Unless, your searchString would be undefined as the user hasn't entered any string initially.

function longestWordFunc(stringArg) {
  console.log(stringArg)
  var stringSplit = stringArg.split(" ");
  console.log(stringSplit)
  var longestWord = 0;
  
  for(var i = 0; i < stringSplit.length; i++){
    if(stringSplit[i].length > longestWord){

      longestWord = stringSplit[i].length;   
    }
    longestWord = stringSplit[i];
   };
  console.log(longestWord)
  console.log(longestWord.length)
  document.getElementsByClassName("longestWord").innerHTML = longestWord;
  document.getElementsByClassName("longestWordCount").innerHTML = longestWord.length;
};
document.querySelector(".generate").addEventListener("click", () => {
  let searchString = document.querySelector(".searchString").value;
  longestWordFunc(searchString);
});
<input type="text" name="searchString" class="searchString">

<span class="longestWord"></span>
<span class="longestWordCount"></span>

<button class="generate">Generate</button>

Sign up to request clarification or add additional context in comments.

3 Comments

This makes sense thank you. Is there a prefered way I could deal with the click event or is this sufficient?
It is sufficient for you. Btw, it is better to get rid of console logs inside the event listeners
Ah great. Yes, I would do that once it's all working sorry. It's okay to use them for testing right?
1

longestWordFunc(searchString) returns undefined, so this line:

document.querySelector(".generate").addEventListener("click", longestWordFunc(searchString));

is getting evaluated as:

document.querySelector(".generate").addEventListener("click", undefined);

And you're not setting anything as the click event listener.

I've fixed your snippet to work below:

function longestWordFunc() {
  let stringArg = document.querySelector(".searchString").value;
  console.log(stringArg);
  var stringSplit = stringArg.split(" ");
  console.log(stringSplit);
  var longestWord = 0;
  
  for(var i = 0; i < stringSplit.length; i++){
    if(stringSplit[i].length > longestWord){

      longestWord = stringSplit[i].length;   
    }
    longestWord = stringSplit[i];
   };
  console.log(longestWord);
  console.log(longestWord.length);
  document.getElementsByClassName("longestWord").innerHTML = longestWord;
  document.getElementsByClassName("longestWordCount").innerHTML = longestWord.length;
};

document.querySelector(".generate").addEventListener("click", longestWordFunc);
<input type="text" name="searchString" class="searchString">

<span class="longestWord"></span>
<span class="longestWordCount"></span>

<button class="generate">Generate</button>

3 Comments

This seems really clean thanks but I dont quite understand it all sorry. Why is longestWordFunc(searchString) undefined? Isn't it being set at let searchString = document.querySelector(".searchString").value;. prior to it being used? Also, isn't document.querySelector(".generate"). what it is listening to?
longestWordFunc doesn't have a return statement so when you invoke it with any value it returns undefined.
You've been really helpful thank you. Could I ask, if you have any advice about how to draw the longest word from my loop? I've fixed the code since the above post so I can find the correct word count of the longest word, but I'm struggling to grab the word itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.