jquery - Error in reading text from textarea in Javascript -


in webpage there textarea (id="text") , button (id="dlbutton3").

what have enter text textarea. , when press button, following happened:

  1. text in text area loaded function,
  2. the text spitted delimiters ""
  3. there loop compare lengths of strings, ,
  4. print longest 1 value

the problem is, following code, can take string text area dun know why cannot split string, , returns error "uncaught referenceerror: targetstring not defined"

the code followed

function findlongestword(){             var testing = document.getelementbyid('text').value;             console.log(testing);             var strtext = testing.split(" ");             var length = 0;             (var i=0; < strtext.length; i++) {                 if (length < strtext[i].length) {                     length = strtext[i].length;                     targetstring = strtext[i]                 }             }             console.log (targetstring);         }         window.onload = function(){             findlongestword();               document.getelementbyid("dlbutton3").onclick = findlongestword;         }  

what can error? many in advance!

read more block scope targetstring exists within loop

function findlongestword() {   var testing = document.getelementbyid('text').value;   var strtext = testing.split(" ");   var length = 0;   var targetstring = '';   (var i=0; < strtext.length; i++) {     if (length < strtext[i].length) {       length = strtext[i].length;       targetstring = strtext[i]     }   }   console.log (targetstring); } window.onload = function() {   findlongestword();   document.getelementbyid("dlbutton3").onclick = findlongestword; } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -