aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes TextArea Value using JS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "TextArea Value using JS" Watch "TextArea Value using JS" New topic
Author

TextArea Value using JS

abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Hi friends,
My problem:

I want to insert a random number in the textarea of my form.

My code:

Javascript:




My HTML code:



But it is not printing my random number in the textarea "token"

Help Me.....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

That's not correct syntax. You can't just put script anywhere in side HTML.

Here's how I'd do it with jQuery:

Life is too short to write JavaScript without jQuery, but if you must you'd write the code in the onload handler, give the input an id value, and use document.getElementById() to find the element.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Also that's not a text area. A textarea is a distinct element of that name. What you are using is a text input.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Thank you....


But i am new to javascripts and would like to use it for these purpose.

Can you help me out...

I tried with getElementById() to acces the element inside <table> but couldn't.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Post your code. We can't see what's wrong with it if you don't post it.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

This is my code:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

You can't just stick script in the value attribute. Remove that stuff.

For now, put the code to set the value of the element in a script block at the bottom of the body.

Later, you can learn about the onload handler.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

ALso, input element don't have innerHTML, you need to set value.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Thank you Bear Bibeault,


Now my code is working. I just added the following code inside body



Thank You again.....
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

OK, while that works, it is not considered a good practice. Modern JavaScript has better ways of accomplishing this (which I have alluded to earlier) and be aware that there are limited places that you can use this technique.

I'm happy that it is working. But be aware that there's more JavaScript that you need to learn if you are going to continue to write web apps.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Thank you ....

suggest me some topics which i must be strong in Javascripts.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Firstly, JavaScript, not Javascripts.

Second, I would grab a book and start learning. Writing JavaScript in the browser is not at all like writing Java programs. Most of JavaScript is event-driven and this is all too big a subject area to cover in a forum post.
abrar alvi
Ranch Hand

Joined: Feb 01, 2012
Posts: 66

Ok I will try my best....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TextArea Value using JS