Page 1 of 1

getElementById

Posted: Thu Aug 13, 2009 10:58 am
by Haddman
I've been working on a very small script, 2 lines to be exact but it refuses to work, i've browsed the internet for tutorials on this and according to them what i'm doing is correct but it doesn't work. I'm a total newbie when it comes to Javascript.

Code: Select all

 
function stringlen(){
    var str=document.getElementById("story");
    alert(str.length);
}
 
When i switch getElementById to something like (var str="12345"; alert(str.length);) it works fine.

Re: getElementById

Posted: Thu Aug 13, 2009 1:34 pm
by kaszu
In your code str is not a String, but HtmlNode, which doesn't have a length property.
Story is input, right? If yes, then it has 'value' property, which is a String.

Code: Select all

function stringlen(){
    var input=document.getElementById("story");
    var str = input.value;
    alert(str.length);
}
 

Re: getElementById

Posted: Thu Aug 13, 2009 1:47 pm
by Haddman
It works now, thanks a lot!