Page 1 of 1
So what is wrong with this code?
Posted: Sat Jun 19, 2010 12:19 am
by KunoNoOni
Code: Select all
<html>
<head>
<script language="javascript" type="text/javascript">
function blabla()
{
document.getElementByName("blatext").value = "This is bla";
}
</script>
</head>
<body>
<form>
<input type="button" value="bla" onClick="blabla();">
<textarea name="blatext"></textarea>
</form>
</body>
</html>
I've even tried
Code: Select all
document.getElementById("blatext").value = "This is bla";
but that doesn't work either. I know its something stupid that I'm overlooking.. so I could use another pair of eyes on this.
-KunoNoOni
Re: So what is wrong with this code?
Posted: Sat Jun 19, 2010 3:03 am
by Jackolantern
Although this is what you said you tried, this worked for me in Firefox and IE:
Code: Select all
<html>
<head>
<script language="javascript" type="text/javascript">
function blabla()
{
var elem = document.getElementById('blatext').value = "This is bla";
}
</script>
</head>
<body>
<form>
<input type="button" value="bla" onClick="blabla()">
<textarea id="blatext"></textarea>
</form>
</body>
</html>
Besides changing the function to getElementById and changing the textarea attribute from name to id, I also removed the ";" from the event hookup in the button tag.
EDIT: Although W3C lists getElementByName is supported in all major browsers, I was getting a "getElementByName is not a function" error in the Firefox error console, so I don't know what the problem is there. However, I mostly use jQuery for Javascript so I don't have to sweat all of those details. You should check it out!
Re: So what is wrong with this code?
Posted: Sat Jun 19, 2010 4:00 pm
by KunoNoOni
I've heard of JQuery but never really learned it. I take a look at it and she if it will help me. thanks
-KunoNoOni
Re: So what is wrong with this code?
Posted: Sat Jun 19, 2010 4:21 pm
by Chris
A textarea doesn't have a value. Notice when you write a textarea you have to write a tag to close it,if you want it to have a value you put it between the two tags. Anything beween tags is its innerHTML.
Code: Select all
document.getElementById("blatext").innerHTML = "This is bla";
Re: So what is wrong with this code?
Posted: Sat Jun 19, 2010 4:49 pm
by Jackolantern
I wonder why it worked then in IE and FF. Maybe it is just being quirky in IE due to no doc type, and then FF was just trying to be nice and gave it to me lol.
Re: So what is wrong with this code?
Posted: Sat Jun 19, 2010 5:17 pm
by Chris
Yeah, it usually does work with value. But I remember once having the same problem, I think in IE6 and this was the problem.