+ 1
i need help please, whay this code don't run ??
<!DOCTYPE html> <html lang="en"> <head> <title>javascript</title> </head> <body> <textarea id="demo"> </textarea> <input type="submit" onclick="myfunc()" value="sent"> <script> function myfunc(){ var mytext = document.getElementById("#demo"); alert(mytext.length); } </script> </body> </html>
2 Réponses
+ 4
And to add to what M. Watney said, you don't need the '#' when you call document.getElementById(), so instead of
document.getElementById("#demo")
call it like this
document.getElementById("demo")
And to display the textarea text length, use this
alert(mytext.value.length)
You missed the `value` attribute there.
+ 3
amir, it's because the JS part gets loaded before the html window and hence it cannot find the html element.
Put your code inside a function which will be called once the html window gets loaded.
window.onload =function () {
//your JS code goes here
}is
And by the next time consider giving a link to your question rather than posting the entire code over here.