So what is wrong with this code?

C++, C#, Java, PHP, ect...
Post Reply
User avatar
KunoNoOni
Posts: 61
Joined: Sun May 09, 2010 1:54 am

So what is wrong with this code?

Post 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
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: So what is wrong with this code?

Post 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!
The indelible lord of tl;dr
User avatar
KunoNoOni
Posts: 61
Joined: Sun May 09, 2010 1:54 am

Re: So what is wrong with this code?

Post 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
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: So what is wrong with this code?

Post 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";
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: So what is wrong with this code?

Post 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.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: So what is wrong with this code?

Post 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.
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Coding”