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:
- text in text area loaded function,
- the text spitted delimiters ""
- there loop compare lengths of strings, ,
- 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
Post a Comment