aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Return string from javascript function and display in text area. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Return string from javascript function and display in text area." Watch "Return string from javascript function and display in text area." New topic
Author

Return string from javascript function and display in text area.

Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 92
Dear Ranchers,

I want to reverse the string "somestring" and display it in the same text area when I click the link "Reverse" in my jsp page. Here is the code that I am working on, but i see no change in my jsp page. I am not sure if the javascript is returning the string. How do I go about doing this, when there is an error in the javascript or in the jsp page where do I go see?How can I display the reversed string in the same text area?




SCJP 6.0
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

First, your JavaScript call will not work--it still needs to use JSP EL notation, and should be in quotes, since it's a JavaScript string.

Second, either in the reverse method itself (boo hiss), or in the method that takes the string as a parameter and calls reverse on it, you need to get the DOM element where you want to put it and set the text of that element.
Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 92
David Newton wrote:
Second, either in the reverse method itself (boo hiss), or in the method that takes the string as a parameter and calls reverse on it, you need to get the DOM element where you want to put it and set the text of that element.




I got the first one I have changed the code for the second one you mentioned, is it correct? But then how do I get the returned string from the javascript function and display it in the text area?

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How do you get the string from the reverse function?How familiar are you with the DOM? Do you know how to get an element from a page? Are you using a JavaScript library, and if so, which one?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm baffled: the DOM part is all here, in one of your previous threads:

http://www.coderanch.com/t/503557/HTML-JavaScript/Change-text

What, specifically, are you having a problem with?
Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 92
My specific problem, how to return the reversed string from the javascript to the jsp.



I dont use any javascript library. it is a jsp page, where my javascript function takes care of the reversing. The problem is I am able to display the reversed string using . But that is not what I want I want to return the string



to the onclick event that calls the javascript function


and display the string in the same text area


that originally had them. Hope I am clear with my problem.

Thanks for your reply.

Romeo Ranjan
Ranch Hand

Joined: Feb 23, 2009
Posts: 92
Ok I got information on how to store the returned string in a variable.



But the above code would give me an alert of the reversed string when I click the link Reverse. But what I need is display the reversed string in the same text area, that once had the original string.. Any hint please.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I already *gave* you hints, and directed you to one of your own threads where you ask how to set the text of a DOM element. Set the text of the DOM element with the reversed string.
K West
Ranch Hand

Joined: Sep 10, 2008
Posts: 58

Try this,

There are few changes in your function.



As David suggested read more about DOM to understand how document.getElementById('mytextarea').innerHTML works. Here is one place W3Schools


Years teach us more than books.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please don't provide complete answers, rather guide people towards being able to figure it out for themselves. People learn much, much better that way.
 
Don't get me started about those stupid light bulbs.
 
subject: Return string from javascript function and display in text area.
 
Similar Threads
Not able to hide a sentence using jquery
pop up window to display an array object using Tomcat
Problem in AJAX and JSP data exchange
pop up window to display an array object
How to detect escape key press in browser