• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TextArea Value using JS

 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code. We can't see what's wrong with it if you don't post it.
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my code:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ALso, input element don't have innerHTML, you need to set value.
 
abrar alvi
Ranch Hand
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you ....

suggest me some topics which i must be strong in Javascripts.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64717
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I will try my best....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic