getElementById

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Haddman
Forum Newbie
Posts: 8
Joined: Tue Aug 04, 2009 9:01 pm

getElementById

Post 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.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: getElementById

Post 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);
}
 
Haddman
Forum Newbie
Posts: 8
Joined: Tue Aug 04, 2009 9:01 pm

Re: getElementById

Post by Haddman »

It works now, thanks a lot!
Post Reply