0
Cannot read value of textarea?
Please someone take a look at my program I just made for testing called "NotFunctioning", why can't it read the textarea? EDIT: Might be a SoloLearn bug? Same thing worked in jsfiddle.net
7 Answers
+ 21
Your textarea has innerHTML but not value.....
replace .value with .innerHTML or replace textarea with :
<textarea id="..." value="test"></textarea>
+ 10
You were right, it's kind of one of sololearn's limitations or advantages of js fiddle
the point is
sololearn tries to load js before the html, that causes an error because the object doesn't exist yet, js fiddle is smarter and runs the html first
you can fix that by replacing you js code with that:
window.onload = function(){
var text = document.getElementById("mine");
alert(text.value);
}
the only change I made is, I've added a window.onload function, this function runs when the html is ready, that way your text area will be "born" đ first, and they your js code will execute properly
hope this helps
+ 8
In Sololearn Code Playground, the three tabs html, css and js are virtually linking in the head section of html document, so JS scripts placed here, need to wait for page content loading before accessing to elements inside it:
window.addEventListener('load', function() {
var text = document.getElementById("mine");
alert(text.value);
});
or:
window.onload = function() {
var text = document.getElementById("mine");
alert(text.value);
});
or:
window.addEventListener('load', init);
function init() {
var text = document.getElementById("mine");
alert(text.value);
}
and so on...
Anyway, you can access the <textareara> 'value' property, but it's just a way to access the 'innerHTML' one in this case, as the real content of it is in 'innerHTML' and only virtually in 'value' property ^^
+ 6
Give a link to your code ( or make it public, so it can be found in your profile )... or copy-paste it here: how would you get an accurate answer to a not accurate question?
+ 2
That didn't work... If I change it to .innerHTML, it will give the same error, but it won't say 'value' but 'innerHTML' as you can see when you run the code. What else can I do? Thanks for trying to help!
+ 1
ok I changed it, but the event listener doesn't work now... any idea why?
0
Oops, it was private... I already deleted it anyways, I figured it out, thanks!